C编程相关问题 [英] C programming related question Question
本文介绍了C编程相关问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
给出单行C表达式以测试数字是否为2的幂.[不允许循环]?
Give a one-line C expression to test whether a number is a power of 2. [No loops allowed]?
推荐答案
isPow2 = x && !( (x-1) & x );
请参阅此处以获得说明:
http://bytes.com/topic/c/answers/213306- test-whether-number-power-2-a [ ^ ]
See here for an explanation:
http://bytes.com/topic/c/answers/213306-test-whether-number-power-2-a[^]
v && !(v & (v - 1));
来源: http://graphics.stanford.edu/~seander/bithacks.html [ ^ ]
Source: http://graphics.stanford.edu/~seander/bithacks.html[^]
返回(v %2 == 0?true:false);
return (v % 2 == 0 ? true : false);
这篇关于C编程相关问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文