GeeksforGeeks » Interview Questions

Amazon Interview Question for Software Engineer/Developer about CPuzzles

(3 posts)

Tags:

  1. puneet
    guest
    Posted 1 year ago #

    Find out what this function does.

    int foo(int n) {
          int t,count=0;
          t=n;
          while(n)
          {
              count=count+1;
              n=(n-1)&t;
          }
          return count;
    }
    
  2. Dheeraj
    Member
    Posted 1 year ago #

    Let the number we get after flipping all the bits of n after the leftmost set bit is n'.

    The function foo() returns n - n'

    For example if n is 9 (000..1001) then n' would be 6(000..0110). So for 9 fun returns 3 (9 -6)

    For example if n is 7 (000..0111) then n' would be 0(000..0000). So for 7 fun returns 7 (9 -0)

  3. prabhat
    guest
    Posted 4 months ago #

    explain for 20 and 21. i m getting 3 and 7. how??


Reply

You must log in to post.

RSS feed for this topic