什么是最便携的方式来获得在GNU C整数/套最高位 [英] What is the most portable way to get/set highest bit of an integer in GNU C
问题描述
什么是最便携的方式来获得在GNU C?
What is the most portable way to get/set highest bit of an integer in GNU C?
这是彭博社的面试问题。我没有在那个时候给最好的答案。任何人都可以回答这个问题?
This is a Bloomberg interview question. I didn't give best answer at that time. Anyone can answer it?
感谢
推荐答案
如果类型是无符号的,它很容易:
If the type is unsigned, it's easy:
(type)-1-(type)-1/2
有关符号值,我知道没有办法。如果你找到一个办法,但这样回答有这么几种悬而未决的问题:
For signed values, I know no way. If you find a way, it would answer several unanswered questions on SO:
<一个href=\"http://stackoverflow.com/questions/4514572/c-question-off-t-and-other-signed-integer-types-minimum-and-maximum-values\">C问题:off_t(和其它符号整型)最小和最大值
<一个href=\"http://stackoverflow.com/questions/3957252/is-there-any-way-to-compute-the-width-of-an-integer-type-at-compile-time\">Is有什么办法可以计算在编译时整数类型的宽度?
也许别人。
这篇关于什么是最便携的方式来获得在GNU C整数/套最高位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!