HCL Interview Question for Software Engineer / Developers


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 4 vote

If you want to toggle a bit, just XOR it with 1 since 0 ^ 1 = 1; 1 ^ 1 = 0.

size_t toggleBits(size_t num, size_t first, size_t last) {
	size_t mask = ((1 << last) - 1) ^ ((1 << first) - 1);
	return num ^ mask;
}

- LinhHA05 July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

I may be misunderstanding something here, and if I am please disregard, but it seems like this solution is almost correct. Let's keep with the question in the sense that they hypothetically want to toggle bits 11-15, it appears that this solution will only toggle bits 11-14.

I think

((1 << last) - 1)

should be slightly altered to

((1 << (last + 1)) - 1)

As an example, let's take the number: 85499444
[represented in binary as 0000 0101 0001 1000 1001 1110 0011 0100]

Assuming

last = 15

and

first = 11

.

With the original solution of:

size_t mask = ((1 << last) - 1) ^ ((1 << first) - 1);

We get a mask of

0000 0000 0000 0000 0111 1000 0000 0000

As we can see this only has an effect on bits 11-14.

However, with the slight alteration of:

size_t mask = ((1 << (last + 1)) - 1) ^ ((1 << first) - 1);

We get a mask of

0000 0000 0000 0000 1111 1000 0000 0000

Which we can see has an effect on bits 11-15.


That said

0000 0101 0001 1000 1001 1110 0011 0100 ^
0000 0000 0000 0000 1111 1000 0000 0000
==================================
0000 0101 0001 1000 0110 0110 0011 0100

This effectively toggles bits 11-15 of the number 85499444.

TL;DR
Unless I misunderstood, I think the correct solution is actually:

size_t toggleBits(size_t num, size_t first, size_t last) {
	size_t mask = ((1 << (last + 1)) - 1) ^ ((1 << first) - 1);
	return num ^ mask;

}

- datta016 August 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

@LinhHA05, @datta016

Nice idea.

- Murali Mohan August 01, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

This should do the trick in Java

public static int toggleBits(int num, int first, int count) {
	for (int i = first; i < first + count && i < 32; i++) {
		num ^= 1 << i;
	}
	return num;
}

- z.d.donnell July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

x ^= ~(~0<<(32-startpos + 1)) | (~0>>>(endpos+1))

- Murali Mohan July 31, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

size_t toggleBits(size_t num, size_t first, size_t last) {
	size_t mask = (POW(2,(last-first))-1) << last ;
	return num ^ mask;
}

- vishal sr August 01, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

it should be num ^ (((1 << last) - 1) ^ ((1<<(first-1)) - 1))
To achieve toggled bit at 11th position you need to keft shift 11-1 times. otherwise you will get 1 at 12th position.

- ijsumana March 01, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More