检查 C/C++ 中最低有效位 (LSB) 和最高有效位 (MSB) 的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6647783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check value of least significant bit (LSB) and most significant bit (MSB) in C/C++
提问by delaccount992
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?
我需要检查 C/C++ 中整数的最低有效位 (LSB) 和最高有效位 (MSB) 的值。我该怎么做?
回答by Armen Tsirunyan
//int value;
int LSB = value & 1;
Alternatively (which is not theoretically portable, but practically it is - see Steve's comment)
或者(理论上不是便携的,但实际上是 - 见史蒂夫的评论)
//int value;
int LSB = value % 2;
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:
详细信息:第二个公式更简单。% 运算符是余数运算符。如果数字是奇数,则其 LSB 为 1,否则为 0。所以我们检查除以 2 的余数。 第一个公式的逻辑是这样的:二进制数 1 是这样的:
0000...0001
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
and 1 & 0 == 0
如果你对任意数字进行二进制与运算,结果的所有位都将为 0,除了最后一位,因为 0 AND 任何其他都是 0。如果你的数字的最后一位是 1,结果的最后一位将为 1因为1 & 1 == 1
和1 & 0 == 0
Thisis a good tutorial for bitwise operations.
这是一个很好的按位运算教程。
HTH.
哈。
回答by dave
You can do something like this:
你可以这样做:
#include <iostream>
int main(int argc, char **argv)
{
int a = 3;
std::cout << (a & 1) << std::endl;
return 0;
}
This way you AND
your variable with the LSB, because
这样你AND
的 LSB 变量,因为
3: 011
1: 001
in 3-bit representation. So being AND
:
以 3 位表示。所以是AND
:
AND
-----
0 0 | 0
0 1 | 0
1 0 | 0
1 1 | 1
You will be able to know if LSB is 1 or not.
您将能够知道 LSB 是否为 1。
edit:find MSB.
编辑:找到MSB。
First of all read Endianessarticle to agree on what MSB
means. In the following lines we suppose to handle with big-endian notation.
首先阅读Endianess文章以同意什么MSB
意思。在以下几行中,我们假设使用 big-endian 表示法处理。
To find the MSB
, in the following snippet we will focus applying a right shift until the MSB
will be AND
ed with 1
.
Consider the following code:
为了找到MSB
,在下面的代码片段中,我们将重点应用右移,直到MSB
将被AND
编辑1
。考虑以下代码:
#include <iostream>
#include <limits.h>
int main(int argc, char **argv)
{
unsigned int a = 128; // we want to find MSB of this 32-bit unsigned int
int MSB = 0; // this variable will represent the MSB we're looking for
// sizeof(unsigned int) = 4 (in Bytes)
// 1 Byte = 8 bits
// So 4 Bytes are 4 * 8 = 32 bits
// We have to perform a right shift 32 times to have the
// MSB in the LSB position.
for (int i = sizeof(unsigned int) * 8; i > 0; i--) {
MSB = (a & 1); // in the last iteration this contains the MSB value
a >>= 1; // perform the 1-bit right shift
}
// this prints out '0', because the 32-bit representation of
// unsigned int 128 is:
// 00000000000000000000000010000000
std::cout << "MSB: " << MSB << std::endl;
return 0;
}
If you print MSB
outside of the cycle you will get 0
.
If you change the value of a
:
如果您MSB
在循环之外打印,您将获得0
. 如果更改 的值a
:
unsigned int a = UINT_MAX; // found in <limits.h>
MSB
will be 1
, because its 32-bit representation is:
MSB
will 1
,因为它的 32 位表示是:
UINT_MAX: 11111111111111111111111111111111
However, if you do the same thing with a signed integerthings will be different.
然而,如果你用有符号整数做同样的事情,事情就会有所不同。
#include <iostream>
#include <limits.h>
int main(int argc, char **argv)
{
int a = -128; // we want to find MSB of this 32-bit unsigned int
int MSB = 0; // this variable will represent the MSB we're looking for
// sizeof(int) = 4 (in Bytes)
// 1 Byte = 8 bits
// So 4 Bytes are 4 * 8 = 32 bits
// We have to perform a right shift 32 times to have the
// MSB in the LSB position.
for (int i = sizeof(int) * 8; i > 0; i--) {
MSB = (a & 1); // in the last iteration this contains the MSB value
a >>= 1; // perform the 1-bit right shift
}
// this prints out '1', because the 32-bit representation of
// int -128 is:
// 10000000000000000000000010000000
std::cout << "MSB: " << MSB << std::endl;
return 0;
}
As I said in the comment below, the MSB
of a positive integeris always 0
, while the MSB
of a negative integeris always 1
.
正如我在下面的评论说,在MSB
一个的正整数始终0
,而MSB
一个的负整数始终1
。
You can check INT_MAX 32-bit representation:
您可以检查 INT_MAX 32 位表示:
INT_MAX: 01111111111111111111111111111111
Now. Why the cycle uses sizeof()
?
If you simply do the cycle as I wrote in the comment: (sorry for the =
missing in comment)
现在。为什么循环使用sizeof()
?如果你只是按照我在评论中写的那样循环:(抱歉=
评论中遗漏了)
for (; a != 0; a >>= 1)
MSB = a & 1;
you will get 1
always, because C++ won't consider the 'zero-pad bits' (because you specified a != 0
as exit statement) higher than the highest 1
. For example for 32-bit integers we have:
你1
总是会得到,因为 C++ 不会认为“零填充位”(因为你指定a != 0
为退出语句)高于最高1
. 例如对于 32 位整数,我们有:
int 7 : 00000000000000000000000000000111
^ this will be your fake MSB
without considering the full size
of the variable.
int 16: 00000000000000000000000000010000
^ fake MSB
回答by Zdeněk Pavlas
int LSB = value & 1;
int MSB = value >> (sizeof(value)*8 - 1) & 1;
回答by ShadowRanger
Others have already mentioned:
其他人已经提到:
int LSB = value & 1;
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:
获得最低有效位。但是有一种比上面提到的更欺骗的方法来获得 MSB。如果该值已经是有符号类型,只需执行以下操作:
int MSB = value < 0;
If it's an unsigned quantity, cast it to the signed type of the same size, e.g. if value
was declared as unsigned
, do:
如果它是一个无符号数量,则将其转换为相同大小的有符号类型,例如,如果value
被声明为unsigned
,请执行以下操作:
int MSB = (int)value < 0;
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.
是的,正式的,不可移植的,未定义的行为,无论如何。但是在我所知道的每两个补码系统和它们的每个编译器上,它恰好可以工作;毕竟,高位是符号位,所以如果有符号形式是负数,那么 MSB 是 1,如果它是非负数,那么 MSB 是 0。所以很方便,对负数的有符号测试相当于检索最高分。
回答by Malcolm McLean
LSB is easy. Just x & 1.
LSB 很简单。只是 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.
MSSB 有点棘手,因为字节可能不是 8 位,而 sizeof(int) 可能不是 4,并且右侧可能有填充位。
Also, with a signed integer, do you mean the sign bit of the MS value bit.
另外,对于有符号整数,您的意思是 MS 值位的符号位。
If you mean the sign bit, life is easy. It's just x < 0
如果你的意思是符号位,生活很容易。只是 x < 0
If you mean the most significant value bit, to be completely portable.
如果您的意思是最重要的值位,则是完全可移植的。
int answer = 0;
int rack = 1;
int mask = 1;
while(rack < INT_MAX)
{
rack << = 1;
mask << = 1;
rack |= 1;
}
return x & mask;
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.
x & (1 << (sizeof(int) * CHAR_BIT) - 2); 将足够便携,并且您的整数不会有填充位。