邓英超 发表于 2013-7-30 15:43:30

[google面试题]How 
can 
you 
quickly 
determine
 whether
 a 
number 
is 
a
 power
 of 
2?

发表于 2013-7-30 16:14:57

Good answer:Check whether x &(x‐1) is 0.If x is not an even power of 2,thehighest position of x with a1 will also have a1 inx‐1;otherwise,x will be100...0and x‐1 will be 011...1;and’ing them together will return 0.
页: [1]
查看完整版本: [google面试题]How 
can 
you 
quickly 
determine
 whether
 a 
number 
is 
a
 power
 of 
2?