## 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);
}``````

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);``

``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);``

``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);

}

Comment hidden because of low score. Click to expand.
1
of 1 vote

@LinhHA05, @datta016

Nice idea.

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;
}``````

Comment hidden because of low score. Click to expand.
0
of 0 vote

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

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 ;
}``````

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.

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.

### 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.