HCL Interview Question for Software Engineer / Developers


Country: United States
Interview Type: In-Person




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

This explains it better, can be used for swapping block of bits :
graphics.stanford.edu/~seander/bithacks.html#SwappingBitsXOR

Swapping just two bits would be too simple as this can be done simply by checking if they are same or not. If not same then set 1 to 0 and 0 to 1 simply by xoring with ( 1<<position ).
Suppose positions are p and q and integer is n:

! (((n & (1 << p)) >> p) ^ ((n & (1 << q)) >> q)):
	bits are same, no need to swap
else
	n ^= ( 1<<p )
	n ^= ( 1<<q )

- Cerberuz April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There is a bug in the if-stmt. It works ok, if the bits are both set to 0, or if one is set to 1 and the other to 0. When they are both set to 1 though, it will mistakenly try to do a swap. Replace with:

! (((n & (1 << p)) >> p) ^ ((n & (1 << q)) >> q))

- Anonymous April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Thanks, made the correction.

- Cerberuz April 21, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

suppose,we are going to swap 2 bits at position m & n of num X . if bits of position m & n are same then there is no need two swap. if bit at m position is 0 and bit at n position is 1 then add 2^m-2^n in X. if bit at m position is 1 and bit at n position is 0 then add 2^n-2^m in X.

- Anonymous April 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{

int n=5,i=1,j=3;
if( ((n&(1<<i))>>i) ^ (n&(1<<j))>>j ){
n^=(1<<i);
n^=(1<<j);
printf("bits are not same");
printf("the value after swapping the bits:%d",n);
}
else {
printf("bits are same");
}
}

- vamshi.430 December 06, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

How to swape pairs of bits in number ?

- Manish rajput June 06, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
int main()
{

int n=5,i=1,j=3;
if( ((n&(1<<i))>>i) ^ (n&(1<<j))>>j ){
n^=(1<<i);
n^=(1<<j);
printf("bits are not same");
printf("the value after swapping the bits:%d",n);
}
else {
printf("bits are same");
}
}

- Anonymous October 04, 2018 | 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