Wednesday, September 22, 2010

09/22: Bit Operation

  • to check whether a value is a power of 2 (simplest way)
    • n is not 0
    • if((n&(n-1))==0 ) –> n is power of 2

Reason :  if n is power of 2 , it only has one 1 in binary format . so n&n-1==0 except n=0

0 Comments:

Post a Comment

<< Home