Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.]

 

我之前的想法好像是 使用LOG去算 就是  lnx / ln2 然後 跟 int( lnx /ln2) 比較看一不一樣

今天看到一個寫法

if (x && !(x & (x-1)) == 0) 但這裡只有假設 x>=0,那為甚麼不乾脆寫

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

留言

這個網誌中的熱門文章

好貴的東元冷氣維修--馬達啟動電容

台大醫院 婁培人 耳鼻喉科 就診

機車無法充電之整流器壞掉--$650