如何使用按位运算设置或清除前3位?

[英]How do I set or clear the first 3 bits using bitwise operations?


Lets say I have a number like 0x448. In binary this is 0100 0100 1000.

可以说我有一个像0x448的数字。在二进制中,这是0100 0100 1000。

How do I set the bits 1, 2 and 3 to either all 0's or all 1's using bit-wise operations? When I say the first three, I'm counting the rightmost bit as the zero bit.

如何使用逐位运算将位1,2和3设置为全0或全1?当我说前三个时,我将最右边的位数作为零位。

So, for example

所以,例如

Bits as 1's:

比特为1:

b12            b0 
  0100 0100 1110
            ^^^

Bits as 0's:

比特为0:

b12            b0
  0100 0100 0000
            ^^^

I'm guessing that to set them to 1's I use bit-wise OR with a mask of 14 (0x000e)? But if that is the case, how do I do something similar for clearing the bits?

我猜测将它们设置为1我使用bit-wise使用14(0x000e)的掩码?但如果是这种情况,我该如何做清除比特的类似事情呢?


Related:

7 个解决方案

#1


You have the bit setting correct: OR with the mask of the bits you want to set.

您将位设置正确:OR与要设置的位的掩码。

Bit clearing bits is very similar: AND with the ones-complement of the bits you want cleared.

位清除位非常相似:AND与要清零的位的补码。

Example: Word of 0x0448.

示例:字0x0448。

Settings bits 1, 2 and 3 would be Word OR 0x000e:

设置位1,2和3将是字或0x000e:

    0000 0100 0100 1000 = 0x0448
 OR 0000 0000 0000 1110 = 0x000e
    ---- ---- ---- ----
  = 0000 0100 0100 1110 = 0x044e

Clearing bits 1, 2 and 3 would be Word AND 0xfff1:

清除位1,2和3将是Word和0xfff1:

    0000 0100 0100 1000 = 0x0448
AND 1111 1111 1111 0001 = 0xfff1
    ---- ---- ---- ----
  = 0000 0100 0100 0000 = 0x0440

Elaborating on the ones-complement, the AND pattern for clearing is the logical NOT of the OR pattern for setting (each bit reveresed):

详细阐述了补码,用于清除的AND模式是用于设置的OR模式的逻辑NOT(每个位被尊重):

 OR 0000 0000 0000 1110 = 0x000e
AND 1111 1111 1111 0001 = 0xfff1

so you can use your favorite language NOT operation instead of having to figure out both values.

所以你可以使用你最喜欢的语言而不是操作,而不必弄清楚这两个值。

#2


Supposing you have a mask m with bits set to 1 for all the bits you want to set or clear, and 0 otherwise:

假设您为要设置或清除的所有位设置了掩码m,其位设置为1,否则为0:

  • clear bits: x & (~m)
  • 清除位:x&(~m)

  • set bits: x | m
  • 设置位:x |米

  • flip bits: x ^ m
  • 翻转位:x ^ m

If you are only interested in one bit, in position p (starting at 0), the mask is simple to express m = 1 << p

如果你只对一位感兴趣,在位置p(从0开始),掩码很容易表达m = 1 << p

Note that I am using C-style conventions, where:

请注意,我使用的是C风格的约定,其中:

  • ~ is the 1-complement: ~10001010 = 01110101
  • 〜是1补码:~10001010 = 01110101

  • & is the bitwise AND
  • &是按位AND

  • | is the bitwise OR
  • |是按位OR

  • ^ is the bitwise XOR
  • ^是按位异或

  • << is the left bit shift: 10001010 << 2 = 00101000
  • < <是左移位:10001010 << 2="00101000

#3


OR with 1 is always true; AND with 0 is always false. :)

OR与1总是如此; AND与0始终为false。 :)

#4


Assuming your OR 0x14 is correct, clearing would be:

假设您的OR 0x14正确,清除将是:

AND (NOT 0x14)

AND(不是0x14)

#5


for clearing the bits use AND with 0x440

清除位使用AND与0x440

#6


number &= ~0xe

#7


Lets make sure that bits are counted from 0 starting at the right side or least significant bit and going left. Then observe this: 1 <

让我们确保从右侧开始或从最低有效位开始向左计数位。然后观察:1 <

 1 <<3     = 00001000

 1 <<p - 1 puts 1 at all positions up to p, exclusive 
 1 <<3-1=00000111

The last step produced a mask to clear bits from the most significant to p inclusive. You can invert it with tilda to clear the other half. Hope this helps.

最后一步产生了一个掩码,用于清除从最重要到最重要的位。您可以使用tilda将其反转以清除另一半。希望这可以帮助。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/04/15/48f68a423789581e15632336a426a2f9.html



 
  © 2014-2022 ITdaan.com 联系我们: