Saturday, November 26, 2011

One line of code for finding out whether a number is power of 2 or not

(An obnoxious looking piece of code) 

If the following LOC returns true, find out what is x here ?


if((x&&!(x&(x-1))==1)) 


Study the Bit-wise operators well, many problems and contests have questions which become quite easy if these are used !!
 The above code is a simple one line test for testing if a variable entered or passed is a power of 2.

No comments:

Post a Comment