GeeksforGeeks » Interview Questions

Microsoft Interview Question for Software Engineer/Developer (Fresher) about CPuzzles

(11 posts)
  • Started 9 months ago by learner
  • Latest reply from Aashish Barnwal

Tags:

  1. learner
    guest
    Posted 9 months ago #

      int x=123,y=231;
      int t=0;
      int l;
      l=x^y;
      while(l)
      {
          t++;
          l&=l-1;
      }
      printf("%d",t);
    
  2. sabyasachi3.cse
    Member
    Posted 9 months ago #

    4 that is the number of set bits in l .

  3. Anonymous

    Posted 9 months ago #

    plz explain

  4. vikas
    guest
    Posted 8 months ago #

    0
    then only it will come out of while loop :)

  5. vikas
    guest
    Posted 8 months ago #

    ooops, haven't read the qs carefully

  6. vikas
    guest
    Posted 8 months ago #

    123 = 0111 1011
    231 = 1110 0111
    -------------------------
    0110 0011

    n & 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

  7. Manohar Singh
    guest
    Posted 8 months ago #

    answer is 5 not 4......

  8. Sergey
    guest
    Posted 8 months ago #

    Answer is 4 not five.

  9. GANESH
    Member
    Posted 8 months ago #

    true ans is 4

  10. Venki
    Moderator
    Posted 8 months ago #

    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.

  11. Dedicated Programmer
    Member
    Posted 4 months ago #

    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.

RSS feed for this topic