问题描述
如何获取 char
?
的位数我知道 CHAR_BIT
来自 climits
。这被描述为»宏产生用于表示char类型的对象的位数的最大值。«at 。我知道这意味着 char
中的位数,不是吗?
I know about CHAR_BIT
from climits
. This is described as »The macro yields the maximum value for the number of bits used to represent an object of type char.« at Dikumware's C Reference. I understand that means the number of bits in a char
, doesn't it?
结果与 std :: numeric_limits
不知何故? std :: numeric_limits< char> :: digits
正确返回 7
,但不幸的是,因为此值尊重8位字符在这里...
Can I get the same result with std::numeric_limits
somehow? std::numeric_limits<char>::digits
returns 7
correctly but unfortunately, because this value respects the signedness of the 8-bit char here…
推荐答案
如果你想过分具体,可以这样做:
If you want to be overly specific, you can do this:
sizeof(char) * CHAR_BIT
如果你知道你肯定要做sizeof char,它有点过分,因为sizeof(char)被保证为1。
If you know you are definitely going to do the sizeof char, it's a bit overkill as sizeof(char) is guaranteed to be 1.
但是如果你移动到不同的类型,如wchar_t,这将是重要的。
But if you move to a different type such as wchar_t, that will be important.
这篇关于获取char中的位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!