更改位整数 [英] Change a bit of an integer

查看:99
本文介绍了更改位整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个整数

int x = 50;

在二进制,它的

00110010

我怎样才能改变四档位编程?

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

推荐答案

您可以通过-ING或将其与处处为零,除了在第四位的值设置数字的第四位。可以这样做的

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);

同样,您可以通过AND-ING它是一处处除了在第四位的值清零第四位。例如:

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. 什么是行为的&LT;&LT; 运营商在这方面

  2. 什么是的行为AND,OR,和XOR操作员在这里?

  1. What is the behavior of the << operator in this context?
  2. What is the behavior of the AND, OR, and XOR operators here?

在以上三种code片断中,我们使用了&LT;&LT; 运算符来生成一个值。在&LT;&LT; 操作符是按位左移位运算符,取一个值,然后移动它的所有位的步骤,将留下一些数字。在你的情况,我用

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

取值1(其中有二重presentation 1)和那么它的所有位在三个地点移动,在遗漏值填充0,这将创建二进制值 1000 ,它在第四位位集。

现在,为什么

x |= (1u << 3);

设置数的第四位?这与OR运算符是如何工作的事情。在 | = 运营商就像 + = * = 除了按位OR - 这是等同于

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);

那么,为什么的OR-ing X具有二进制值 1000 设置它的第四位?这与该办法做到这一点OR​​定义:

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

这是一个非常重要的事实,因为这意味着的OR-ing零任何位不会改变该位的值,OR-ING与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);

因为(1U&LT; 3;)是处处为零,除了在第四位,该位或离开所有的x,除了第四位,然后将其设置为一不变位的值。更一般地,的OR-ing与值的数字,是一系列的零和一将preserve的所有地方位为零,并设置所有其中位是一个值的值。

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.

现在,让我们来看看

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

本使用按位补运算符,这需要一个数字,翻转所有位。如果我们假设整数是两个字节(只是为简单起见),这意味着它们的实际编码(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

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

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

或者,更简洁:

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

注意,这意味着,如果我们和两个数相加,将得到的值将是这样的,所有的比特以AND零被设置为零,而所有其它位为preserved。这意味着,如果我们,以

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)

我们正在和-ING以

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)

翻转数的第四比特。这是因为二进制XOR运算符具有此真值表:

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

注意

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

其中,〜X 是X的对立面;它是0 1 1 0。这意味着,如果我们XOR X具有值(1U&LT;&LT; 3),我们正在与<异或来/ p>

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

因此​​,这意味着把所有的位,但在第四位设置为是,但翻转第四位。更一般地,如果你要翻转位的一定数目,XOR拥有一批具有要保持完好位要翻转此位0和1的值。

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.

希望这有助于!

这篇关于更改位整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆