C++ 改变一点整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6916974/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:53:33  来源:igfitidea点击:

Change a bit of an integer

c++cbit-manipulationbitwise-operators

提问by pedram

We have an integer number

我们有一个整数

int x = 50;

in binary, it's

在二进制中,它是

00110010

How can I change the fourth (4th) bit programatically?

如何以编程方式更改第四(第 4)位?

回答by templatetypedef

You can set the fourth bit of a number by OR-ing it with a value that is zero everywhere except in the fourth bit. This could be done as

您可以通过将数字与除第四位以外的任何地方都为零的值进行“或”运算来设置数字的第四位。这可以做为

x |= (1u << 3);

Similarly, you can clear the fourth bit by AND-ing it with a value that is one everywhere except in the fourth bit. For example:

类似地,您可以通过与除第四位以外的任何地方都为 1 的值进行 AND 运算来清除第四位。例如:

x &= ~(1u << 3);

Finally, you can toggle the fourth bit by XOR-ing it with a value that is zero everywhere except in the fourth bit:

最后,您可以通过与除第四位以外的任何地方都为零的值进行异或来切换第四位:

x ^= (1u << 3);

To see why this works, we need to look at two things:

要了解为什么会这样,我们需要看两件事:

  1. What is the behavior of the <<operator in this context?
  2. What is the behavior of the AND, OR, and XOR operators here?
  1. <<在这种情况下,操作员的行为是什么?
  2. AND、OR 和 XOR 运算符在这里的行为是什么?

In all three of the above code snippets, we used the <<operator to generate a value. The <<operator is the bitwise shift-left operator, which takes a value and then shifts all of its bits some number of steps to the left. In your case, I used

在上述所有三个代码片段中,我们都使用了<<运算符来生成一个值。该<<运算符是按位左移运算符,它接受一个值,然后将其所有位向左移动一定数量的步数。在你的情况下,我用

1u << 3

to take the value 1 (which has binary representation 1) and to then shift all its bits over three spots, filling in the missing values with 0. This creates the binary value 1000, which has a bit set in the fourth bit.

取值 1(二进制表示为 1),然后将其所有位移位三个点,用 0 填充缺失值。这将创建二进制值1000,它在第四位设置了一个位。

Now, why does

现在,为什么

x |= (1u << 3);

set the fourth bit of the number? This has to do with how the OR operator works. The |=operator is like +=or *=except for bitwise OR - it's equivalent to

设置数字的第四位?这与 OR 运算符的工作方式有关。该|=操作是像+=或者*=除了按位或-这是等同于

x = x | (1u << 3);

So why does OR-ing x with the binary value 1000set its fourth bit? This has to do with the way that OR is defined:

那么为什么将 x 与二进制值进行 OR-ing1000设置其第四位呢?这与 OR 的定义方式有关:

0 | 0  == 0
0 | 1  == 1
1 | 0  == 1
1 | 1  == 1

More importantly, though, we can rewrite this more compactly as

更重要的是,我们可以更紧凑地将其重写为

x | 0  == x
x | 1  == 1

This is an extremely important fact, because it means that OR-ing any bit with zero doesn't change the bit's value, while OR-ing any bit with 1 always sets that bit to one. This means that when we write

这是一个极其重要的事实,因为这意味着对任何位与零进行 OR 运算不会改变该位的值,而对任何位与 1 进行 OR 运算总是将该位设置为 1。这意味着当我们写

x |= (1u << 3);

since (1u << 3) is a value that is zero everywhere except in the fourth bit, the bitwise OR leaves all the bits of x unchanged except for the fourth bit, which is then set to one. More generally, OR-ing a number with a value that is a series of zeros and ones will preserve all the values where the bits are zero and set all of the values where the bits are one.

由于 (1u << 3) 是一个值,除了第四位之外,其他任何地方都为零,因此按位 OR 使 x 的所有位保持不变,除了第四位,然后将其设置为 1。更一般地,将一个数字与一系列零和一的值进行 OR 运算将保留所有位为零的值,并设置所有位为 1 的值。

Now, let's look at

现在,让我们看看

x &= ~(1u << 3);

This uses the bitwise complement operator ~, which takes a number and flips all of its bits. If we assume that integers are two bytes (just for simplicity), this means that the actual encoding of (1u << 3)is

这使用按位补码运算符~,它接受一个数字并翻转它的所有位。如果我们假设整数是两个字节(只是为了简单起见),这意味着 的实际编码(1u << 3)

0000000000001000

When we take the complement of this, we get the number

当我们取这个的补码时,我们得到数字

1111111111110111

Now, let's see what happens when we bitwise AND two values together. The AND operator has this interesting truth table:

现在,让我们看看当我们将两个值按位 AND 在一起时会发生什么。AND 运算符有一个有趣的真值表:

0 & 0   == 0
0 & 1   == 0
1 & 0   == 0
1 & 1   == 1

Or, more compactly:

或者,更简洁:

x & 0   == 0
x & 1   == x

Notice that this means that if we AND two numbers together, the resulting value will be such that all of the bits AND-ed with zero are set to zero, while all other bits are preserved. This means that if we AND with

请注意,这意味着如果我们将两个数字 AND 在一起,结果值将是所有与零进行 AND 运算的位都设置为零,而所有其他位都保留。这意味着如果我们与

~(1u << 3)

we are AND-ing with

我们正在与

1111111111110111

So by our above table, this means "keep all of the bits, except for the fourth bit, as-is, and then change the fourth bit to be zero."

因此,根据我们的上表,这意味着“保留除第四位之外的所有位,原样,然后将第四位更改为零。”

More generally, if you want to clear a set of bits, create a number that is one everywhere you want to keep the bits unchanged and zero where you want to clear the bits.

更一般地,如果您想清除一组位,请创建一个数字,该数字在您希望保持这些位不变的任何地方都为 1,而在您希望清除这些位的地方为 0。

Finally, let's see why

最后,让我们看看为什么

x ^= (1u << 3)

Flips the fourth bit of the number. This is because the binary XOR operator has this truth table:

翻转数字的第四位。这是因为二元异或运算符有这个真值表:

0 ^ 0  == 0
0 ^ 1  == 1
1 ^ 0  == 1
1 ^ 1  == 0

Notice that

请注意

x ^ 0  == 0
x ^ 1  == ~x

Where ~xis the opposite of x; it's 0 for 1 and 1 for 0. This means that if we XOR x with the value (1u << 3), we're XOR-ing it with

哪里~x是 x 的反面;它是 0 代表 1 和 1 代表 0。这意味着,如果我们将 x 与值进行(1u << 3)异或,我们将对其进行异或

0000000000001000

So this means "keep all the bits but the fourth bit set as is, but flip the fourth bit." More generally, if you want to flip some number of bits, XOR the value with a number that has zero where you want to keep the bits intact and one where you want to flip this bits.

所以这意味着“保持除第四位以外的所有位保持原样,但翻转第四位。” 更一般地,如果您想翻转一些位,请将值与一个数字进行异或,该数字在您希望保持位完整的位置为零,在您希望翻转这些位的位置为一个。

Hope this helps!

希望这可以帮助!

回答by Mark B

You can always use std::bitsetwhich makes modifying bits easy.

您始终可以使用std::bitset它使修改位变得容易。

Or you can use bit manipulations (assuming you mean 4th bit counting at one. Don't subtract 1 if you mean counting from 0). Note that I use 1Ujust to guarantee that the whole operation happens on unsigned numbers:

或者您可以使用位操作(假设您的意思是第 4 位计数为 1。如果您的意思是从 0 计数,则不要减去 1)。请注意,我1U只是为了保证整个操作发生在无符号数上:

To set: x |= (1U << (4 - 1));

设置: x |= (1U << (4 - 1));

To clear: x &= ~(1U << (4 - 1));

清除: x &= ~(1U << (4 - 1));

To toggle: x ^= (1U << (4 - 1));

切换: x ^= (1U << (4 - 1));

回答by Patrick87

To set the fourth bit, ORwith 00001000(binary).

设置第四位,OR00001000(二进制)。

To clear the fourth bit, ANDwith 11110111(binary).

清除第四位,AND11110111(二进制)。

To toggle the fourth bit, XORwith 00001000(binary).

要切换第四位,XOR使用00001000(二进制)。

Examples:

例子:

00110010 OR 00001000 = 00111010

00110010 AND 11110111 = 00110010

00110010 XOR 00001000 = 00111010

00110010 或 00001000 = 00111010

00110010 和 11110111 = 00110010

00110010 异或 00001000 = 00111010

回答by Dr. Debasish Jana

Simple, since you have, or whatever value you have,

很简单,因为你拥有,或者你拥有的任何价值,

int x = 50;

To set 4th bit (from right) programatically,

要以编程方式设置第 4 位(从右起),

int y = x | 0x00000008;

Because, 0xprefixed before a number means it's hexadecimal form. So, 0x0 = 0000in binary, and 0x8=1000in binary form. That explains the answer.

因为,0x在数字之前加前缀意味着它是十六进制形式。所以,0x0 = 00000x8=1000二进制形式,以二进制形式。这解释了答案。

回答by Vincet

Try one of these functions in C language to change n bit

试试 C 语言中的这些函数之一来改变 n 位

char bitfield;

// start at 0th position

void chang_n_bit(int n, int value)
{
    bitfield = (bitfield | (1 << n)) & (~( (1 << n) ^ (value << n) ));
}

void chang_n_bit(int n, int value)
{
    bitfield = (bitfield | (1 << n)) & ((value << n) | ((~0) ^ (1 << n)));
}

void chang_n_bit(int n, int value)
{
    if(value)
        bitfield |= 1 << n;
    else
        bitfield &= ~0 ^ (1 << n);
}

char print_n_bit(int n)
{
    return (bitfield & (1 << n)) ? 1 : 0;
}

回答by Dustin Howett

You can use binary AND and OR to toggle the fourth bit.

您可以使用二进制 AND 和 OR 来切换第四位。

To set the fourth bit on x, you would use x |= 1<<3;, 1<<3being a left shift of 0b0001 by three bits producing 0b1000.

要在 x 上设置第四位,您可以使用x |= 1<<3;1<<3即 0b0001 左移三位产生 0b1000。

To clear the fourth bit on x, you would use x &= ~(1<<3);, a binary AND between 0b00110010 (x) and (effectively) 0b11110111, masking out every bit in x that is not in position four, thus clearing it.

要清除 x 上的第四位,您将使用x &= ~(1<<3);0b00110010 (x) 和(有效)0b11110111 之间的二进制 AND,屏蔽掉 x 中不在位置四的每一位,从而清除它。