What would this function return

This is a discussion on What would this function return within the C Programming forums, part of the General Programming Boards category; Code: What would this function return: int foo(int a) { return !(a & (a - 1)) && a; } a)Returns ...

  1. #1
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Question What would this function return

    Code:
    What would this function return:
    int foo(int a)
    {
      return !(a & (a - 1)) && a;
    }
    
    
    a)Returns non zero when a is odd
    b)Returns non zero when a is even
    c)Returns non zero when a is power of 2
    d)Always return zero

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    Compile it, try it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    C is Sea. I know a drop! ganesh bala's Avatar
    Join Date
    Jan 2009
    Location
    Bangalore
    Posts
    58

    Talking

    return !(a & (a - 1)) && a;
    >> Returns non zero when a is power of 2

    a&a-1 will be zero only when a is 1,2,4,8,16,32 ...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21