Hacking with Bits in C
>> Saturday, October 18, 2008
Recently one of my friends came and ask me this interesting question.
"Write a one line C code to check whether a given number is a power of two. Can you do that within 10 mins?"
Here is the answer : bool f = !(v & (v - 1)) && v
If you happen to have the answer bool f = (v & (v - 1)) == 0 , it will give 0 also as an answer, which is wrong.
I then found more like these, bit hacking codes, here.

0 comments:
Post a Comment