GeeksforGeeks » Interview Questions
Microsoft Interview Question for Software Engineer/Developer (Fresher) about CPuzzles
(11 posts)-
int x=123,y=231; int t=0; int l; l=x^y; while(l) { t++; l&=l-1; } printf("%d",t); -
4 that is the number of set bits in l .
-
plz explain
-
0
then only it will come out of while loop :) -
ooops, haven't read the qs carefully
-
123 = 0111 1011
231 = 1110 0111
-------------------------
0110 0011n & n-1 is in general used to check if number is power of 2 or not
this when n is decremented by 1, rightmost bit in n will be zero and trailing will be 1 and their AND produces a number which is less 1 bit set
thus answer is 4 -
answer is 5 not 4......
-
Answer is 4 not five.
-
true ans is 4
-
EXOR operation results 1 when inputs are inverse each other. After the expression l = x ^ y, l contains those bits in which x and y differ.
The next loop is to count number of set bits in l.
Overall, we will get number of bits that are dissimilar in X and Y.
-
Answer is 4 as program outputs numbe of 1's in the binary representation of the result.
XOR operation outputs 1 is both bits are inverse of each other,
After XORing, output is 10011100
so number of 1's is 4.
Reply
You must log in to post.