更改整数的一位

发布于 2024-11-27 13:30:08 字数 129 浏览 1 评论 0原文

整数

int x = 50;

我们有一个二进制

00110010

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

We have an integer number

int x = 50;

in binary, it's

00110010

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

指尖微凉心微凉 2024-12-04 13:30:08

您可以通过将数字与除了第四位之外的所有位置都为零的值进行“或”运算来设置数字的第四位。类似地

x |= (1u << 3);

,您可以通过将第四位与除第四位之外的所有位置都为 1 的值进行 AND 运算来清除第四位。例如:

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

最后,您可以通过将第四位与除第四位之外的所有位置都为零的值进行异或来切换第四位:

x ^= (1u << 3);

要了解其工作原理,我们需要了解两件事:

  1. < 的行为是什么code><< 运算符在此上下文中?
  2. 这里 AND、OR 和 XOR 运算符的行为是什么?

在上述所有三个代码片段中,我们使用 << 运算符来生成值。 << 运算符是按位左移运算符,它接受一个值,然后将其所有位向左移动一定步数。在您的例子中,我曾经

1u << 3

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

现在,为什么要

x |= (1u << 3);

设置数字的第四位呢?这与 OR 运算符的工作方式有关。 |= 运算符类似于 +=*=,除了按位 OR - 它相当于

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

So Why does OR-ing x with the binary值 1000 设置其第四位?这与 OR 的定义方式有关:

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

不过,更重要的是,我们可以更紧凑地重写它,因为

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

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

x |= (1u << 3);

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

现在,让我们看看

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

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

0000000000001000

数字

1111111111110111

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

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

或者,更紧凑地说:

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

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

~(1u << 3)

与上表中的 So进行 AND 运算

1111111111110111

,则意味着“按原样保留除第四位之外的所有位,然后将第四位更改为零”。

更一般而言,如果您想要清除一组位,请创建一个数字,该数字在您想要保持位不变的地方为 1,在您想要清除位的地方为 0。

最后我们看看为什么

x ^= (1u << 3)

要翻转数字的第四位。这是因为二元 XOR 运算符具有以下真值表:

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

注意

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

其中 ~x 与 x 相反; 0 代表 1,1 代表 0。这意味着,如果我们将 x 与值 (1u << 3) 进行异或,我们将与它进行异或

0000000000001000

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

希望这有帮助!

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:

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?

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.

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

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

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

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

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.

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

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:

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

~(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.

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 ~x is 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

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!

笑着哭最痛 2024-12-04 13:30:08

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

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

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

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

x ^= (1U << (4 - 1));

You can always use std::bitset which makes modifying bits easy.

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 1U just to guarantee that the whole operation happens on unsigned numbers:

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

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

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

迷迭香的记忆 2024-12-04 13:30:08

要设置第四位,请与 00001000(二进制)进行OR

要清除第四位,请与 11110111(二进制)进行AND

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

示例:

00110010 或 00001000 = 00111010

00110010 和 11110111 = 00110010

00110010 异或 00001000 = 00111010

To set the fourth bit, OR with 00001000 (binary).

To clear the fourth bit, AND with 11110111 (binary).

To toggle the fourth bit, XOR with 00001000 (binary).

Examples:

00110010 OR 00001000 = 00111010

00110010 AND 11110111 = 00110010

00110010 XOR 00001000 = 00111010

只为守护你 2024-12-04 13:30:08

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

int x = 50;

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

int y = x | 0x00000008;

因为,数字前面的 0x 前缀意味着它是十六进制形式。
因此,二进制形式的 0x0 = 0000 和二进制形式的 0x8=1000
这就解释了答案。

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

int x = 50;

To set 4th bit (from right) programatically,

int y = x | 0x00000008;

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

策马西风 2024-12-04 13:30:08

尝试使用 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;
}

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

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;
}
悟红尘 2024-12-04 13:30:08

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

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

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

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

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

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