检查 C/C++ 中最低有效位 (LSB) 和最高有效位 (MSB) 的值
我需要检查 C/C++ 中整数的最低有效位 (LSB) 和最高有效位 (MSB) 的值。我该怎么做?
I need to check the value of the least significant bit (LSB) and most significant bit (MSB) of an integer in C/C++. How would I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
或者(理论上不可移植,但实际上可以 - 请参阅史蒂夫的评论)
详细信息:
第二个公式更简单。 % 运算符是余数运算符。当一个数字是奇数时,其 LSB 为 1,否则为 0。所以我们检查除以 2 的余数。 第一个公式的逻辑是这样的: 二进制中的数字 1 是这样的:
如果将它与任意数字进行二元与,则结果除了最后一位之外的所有位都将为 0因为 0 AND 其他任何值都是 0。当且仅当您的数字的最后一位为 1 时,结果的最后一位将为 1,因为
1 & 1 == 1
和1 & 0 == 0
这是一个很好的按位运算教程。
HTH。
Alternatively (which is not theoretically portable, but practically it is - see Steve's comment)
Details:
The second formula is simpler. The % operator is the remainder operator. A number's LSB is 1 iff it is an odd number and 0 otherwise. So we check the remainder of dividing with 2. The logic of the first formula is this: number 1 in binary is this:
If you binary-AND this with an arbitrary number, all the bits of the result will be 0 except the last one because 0 AND anything else is 0. The last bit of the result will be 1 iff the last bit of your number was 1 because
1 & 1 == 1
and1 & 0 == 0
This is a good tutorial for bitwise operations.
HTH.
您可以执行以下操作:
这样您就可以将变量与 LSB 进行“与”操作,因为
采用的是 3 位表示形式。因此,
AND
:您将能够知道 LSB 是否为 1。
编辑:找到MSB。
首先阅读 Endianess 文章以就
MSB
的含义达成一致。在下面的行中,我们假设使用大端表示法进行处理。为了找到
MSB
,在下面的代码片段中,我们将重点应用右移,直到MSB
与1< 进行
AND
运算。 /代码>。考虑以下代码:
如果在循环之外打印
MSB
,您将得到0
。如果您更改
a
的值:MSB
将是1
,因为它的 32 位表示为:但是,如果您执行相同的操作使用有符号整数情况会有所不同。
正如我在下面的评论中所说,正整数的
MSB
始终为0
,而MSB
负整数始终为1
。您可以检查 INT_MAX 32 位表示:
现在。为什么循环使用
sizeof()
?如果你只是按照我在评论中写的方式进行循环:(抱歉评论中缺少
=
),你将始终得到
1
,因为 C++ 不会考虑 '零填充位”(因为您指定了a != 0
作为退出语句)高于最高的1
。例如,对于 32 位整数,我们有:You can do something like this:
This way you
AND
your variable with the LSB, becausein 3-bit representation. So being
AND
:You will be able to know if LSB is 1 or not.
edit: find MSB.
First of all read Endianess article to agree on what
MSB
means. In the following lines we suppose to handle with big-endian notation.To find the
MSB
, in the following snippet we will focus applying a right shift until theMSB
will beAND
ed with1
.Consider the following code:
If you print
MSB
outside of the cycle you will get0
.If you change the value of
a
:MSB
will be1
, because its 32-bit representation is:However, if you do the same thing with a signed integer things will be different.
As I said in the comment below, the
MSB
of a positive integer is always0
, while theMSB
of a negative integer is always1
.You can check INT_MAX 32-bit representation:
Now. Why the cycle uses
sizeof()
?If you simply do the cycle as I wrote in the comment: (sorry for the
=
missing in comment)you will get
1
always, because C++ won't consider the 'zero-pad bits' (because you specifieda != 0
as exit statement) higher than the highest1
. For example for 32-bit integers we have:其他人已经提到:
获取最低有效位。但还有一种比上面提到的更欺骗的方法来获取 MSB。如果该值已经是有符号类型,只需执行以下操作:
如果它是无符号数量,则将其转换为相同大小的有符号类型,例如,如果
value
被声明为unsigned
, do:是的,正式的,不可移植的,未定义的行为,等等。但在我所知道的每个二进制补码系统和每个编译器上,它恰好可以工作;毕竟,高位是符号位,因此如果有符号形式为负数,则 MSB 为 1,如果为非负数,则 MSB 为 0。所以方便地,负数的有符号测试相当于检索MSB。
Others have already mentioned:
for getting the least significant bit. But there is a cheatier way to get the MSB than has been mentioned. If the value is a signed type already, just do:
If it's an unsigned quantity, cast it to the signed type of the same size, e.g. if
value
was declared asunsigned
, do:Yes, officially, not portable, undefined behavior, whatever. But on every two's complement system and every compiler for them that I'm aware of, it happens to work; after all, the high bit is the sign bit, so if the signed form is negative, then the MSB is 1, if it's non-negative, the MSB is 0. So conveniently, a signed test for negative numbers is equivalent to retrieving the MSB.
LSB 很容易。只是 x & 1.
MSSB 有点棘手,因为 bytes 可能不是 8 位,sizeof(int) 可能不是 4,并且右侧可能有填充位。
另外,对于有符号整数,您的意思是MS值位的符号位。
如果你指的是符号位,那么生活就很容易了。只是 x < 0
如果您指的是最高有效值位,则完全可移植。
这是一种冗长的做法。实际上
x & (1 << (sizeof(int) * CHAR_BIT) - 2);
将具有足够的可移植性,并且您的整数不会有填充位。
LSB is easy. Just x & 1.
MSSB is a bit trickier, as bytes may not be 8 bits and sizeof(int) may not be 4, and there might be padding bits to the right.
Also, with a signed integer, do you mean the sign bit of the MS value bit.
If you mean the sign bit, life is easy. It's just x < 0
If you mean the most significant value bit, to be completely portable.
That's a long-winded way of doing it. In reality
x & (1 << (sizeof(int) * CHAR_BIT) - 2);
will be quite portable enough and your ints won't have padding bits.