检查 C/C++ 中最低有效位 (LSB) 和最高有效位 (MSB) 的值

发布于 2024-11-19 09:59:47 字数 57 浏览 6 评论 0原文

我需要检查 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何其悲哀 2024-11-26 09:59:47
//int value;
int LSB = value & 1;

或者(理论上不可移植,但实际上可以 - 请参阅史蒂夫的评论)

//int value;
int LSB = value % 2;

详细信息:
第二个公式更简单。 % 运算符是余数运算符。当一个数字是奇数时,其 LSB 为 1,否则为 0。所以我们检查除以 2 的余数。 第一个公式的逻辑是这样的: 二进制中的数字 1 是这样的:

0000...0001

如果将它与任意数字进行二元与,则结果除了最后一位之外的所有位都将为 0因为 0 AND 其他任何值都是 0。当且仅当您的数字的最后一位为 1 时,结果的最后一位将为 1,因为 1 & 1 == 11 & 0 == 0

这是一个很好的按位运算教程。

HTH。

//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:

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

This is a good tutorial for bitwise operations.

HTH.

只为一人 2024-11-26 09:59:47

您可以执行以下操作:

#include <iostream>

int main(int argc, char **argv)
{
    int a = 3;
    std::cout << (a & 1) << std::endl;
    return 0;
}

这样您就可以将变量与 LSB 进行“与”操作,因为

3: 011
1: 001

采用的是 3 位表示形式。因此,AND

AND
-----
0  0  | 0
0  1  | 0
1  0  | 0
1  1  | 1

您将能够知道 LSB 是否为 1。

编辑:找到MSB。

首先阅读 Endianess 文章以就 MSB 的含义达成一致。在下面的行中,我们假设使用大端表示法进行处理。

为了找到 MSB,在下面的代码片段中,我们将重点应用右移,直到 MSB1< 进行 AND 运算。 /代码>。
考虑以下代码:

#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;
}

如果在循环之外打印 MSB,您将得到 0
如果您更改 a 的值:

unsigned int a = UINT_MAX; // found in <limits.h>

MSB 将是 1,因为它的 32 位表示为:

UINT_MAX: 11111111111111111111111111111111

但是,如果您执行相同的操作使用有符号整数情况会有所不同。

#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;
}

正如我在下面的评论中所说,正整数MSB始终为0,而MSB 负整数始终为1

您可以检查 INT_MAX 32 位表示:

INT_MAX: 01111111111111111111111111111111

现在。为什么循环使用sizeof()
如果你只是按照我在评论中写的方式进行循环:(抱歉评论中缺少 =),

for (; a != 0; a >>= 1)
    MSB = a & 1;

你将始终得到 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

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

3: 011
1: 001

in 3-bit representation. So being 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.

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 the MSB will be ANDed with 1.
Consider the following code:

#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:

unsigned int a = UINT_MAX; // found in <limits.h>

MSB will be 1, because its 32-bit representation is:

UINT_MAX: 11111111111111111111111111111111

However, if you do the same thing with a signed integer things 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 integer is always 0, while the MSB of a negative integer is always 1.

You can check INT_MAX 32-bit representation:

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)

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:

int 7 : 00000000000000000000000000000111
                                     ^ this will be your fake MSB
                                       without considering the full size 
                                       of the variable.

int 16: 00000000000000000000000000010000
                                   ^ fake MSB
她比我温柔 2024-11-26 09:59:47
int LSB = value & 1;
int MSB = value >> (sizeof(value)*8 - 1) & 1;
int LSB = value & 1;
int MSB = value >> (sizeof(value)*8 - 1) & 1;
韵柒 2024-11-26 09:59:47

其他人已经提到:

int LSB = value & 1;

获取最低有效位。但还有一种比上面提到的更欺骗的方法来获取 MSB。如果该值已经是有符号类型,只需执行以下操作:

int MSB = value < 0;

如果它是无符号数量,则将其转换为相同大小的有符号类型,例如,如果 value 被声明为 unsigned , do:

int MSB = (int)value < 0;

是的,正式的,不可移植的,未定义的行为,等等。但在我所知道的每个二进制补码系统和每个编译器上,它恰好可以工作;毕竟,高位是符号位,因此如果有符号形式为负数,则 MSB 为 1,如果为非负数,则 MSB 为 0。所以方便地,负数的有符号测试相当于检索MSB。

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:

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:

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.

尝蛊 2024-11-26 09:59:47

LSB 很容易。只是 x & 1.

MSSB 有点棘手,因为 bytes 可能不是 8 位,sizeof(int) 可能不是 4,并且右侧可能有填充位。

另外,对于有符号整数,您的意思是MS值位的符号位。

如果你指的是符号位,那么生活就很容易了。只是 x < 0

如果您指的是最高有效值位,则完全可移植。

 int answer  = 0;
 int rack = 1;
 int mask  = 1;

 while(rack < INT_MAX)
 {
    rack << = 1;
    mask << = 1;
    rack |= 1; 
 } 

 return x & mask;

这是一种冗长的做法。实际上

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.

 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文