Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

c++ - 如何设置,清除和切换单个位?(How do you set, clear, and toggle a single bit?)

您如何设置,清除和切换?

  ask by JeffV translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Setting a bit (设置一点)

Use the bitwise OR operator ( | ) to set a bit. (使用按位或运算符( | )进行设置。)

number |= 1UL << n;

That will set the n th bit of number . (这将设置numbern位。) n should be zero, if you want to set the 1 st bit and so on upto n-1 , if you want to set the n th bit. (n应该是零,如果你想设置1 ST位,依此类推高达n-1如果你想设置的n个位。)

Use 1ULL if number is wider than unsigned long ; (如果number大于unsigned long则使用1ULL ;) promotion of 1UL << n doesn't happen until after evaluating 1UL << n where it's undefined behaviour to shift by more than the width of a long . (直到对1UL << n进行评估后,才发生1UL << n提升,在这种情况下,未定义的行为是将偏移的长度大于long的宽度。) The same applies to all the rest of the examples. (其他所有示例也是如此。)

Clearing a bit (清除一点)

Use the bitwise AND operator ( & ) to clear a bit. (使用按位AND运算符( & )清除一位。)

number &= ~(1UL << n);

That will clear the n th bit of number . (这将清除n个位number 。) You must invert the bit string with the bitwise NOT operator ( ~ ), then AND it. (您必须使用按位NOT运算符( ~ )反转位字符串,然后将其取反。)

Toggling a bit (切换一点)

The XOR operator ( ^ ) can be used to toggle a bit. (XOR运算符( ^ )可用于切换一位。)

number ^= 1UL << n;

That will toggle the n th bit of number . (这将切换n的个位number 。)

Checking a bit (检查一下)

You didn't ask for this, but I might as well add it. (您没有要求这样做,但我也可以添加它。)

To check a bit, shift the number n to the right, then bitwise AND it: (要检查一点,请将数字n右移,然后按位与它相乘:)

bit = (number >> n) & 1U;

That will put the value of the n th bit of number into the variable bit . (这将把数字的第n位的number放入可变bit 。)

Changing the n th bit to x (将第n位更改为x)

Setting the n th bit to either 1 or 0 can be achieved with the following on a 2's complement C++ implementation: (在2的补码C ++实现中,可以通过以下操作将第n位设置为10 :)

number ^= (-x ^ number) & (1UL << n);

Bit n will be set if x is 1 , and cleared if x is 0 . (如果x1 ,则将设置n位;如果x0 ,则将其清除。) If x has some other value, you get garbage. (如果x具有其他值,则会产生垃圾。) x = !!x will booleanize it to 0 or 1. (x = !!x会将其布尔值设为0或1。)

To make this independent of 2's complement negation behaviour (where -1 has all bits set, unlike on a 1's complement or sign/magnitude C++ implementation), use unsigned negation. (为了使它独立于2的补码取反行为(其中-1设置了所有位,与1的补码或符号/幅度C ++实现不同),请使用无符号取反。)

number ^= (-(unsigned long)x ^ number) & (1UL << n);

or (要么)

unsigned long newbit = !!x;    // Also booleanize to force 0 or 1
number ^= (-newbit ^ number) & (1UL << n);

It's generally a good idea to use unsigned types for portable bit manipulation. (使用无符号类型进行可移植位操作通常是一个好主意。)

or (要么)

number = (number & ~(1UL << n)) | (x << n);

(number & ~(1UL << n)) will clear the n th bit and (x << n) will set the n th bit to x . ((number & ~(1UL << n))将清除第n位,而(x << n)将第n位设置为x 。)

It's also generally a good idea to not to copy/paste code in general and so many people use preprocessor macros (like the community wiki answer further down ) or some sort of encapsulation. (通常不要复制/粘贴代码也是一个好主意,因此许多人使用预处理器宏(例如社区Wiki进一步回答 )或某种封装形式。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...