10

Let’s say I have a number like 0x448. In binary this is 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.

So, for example

Bits as 1's:

b12            b0
  0100 0100 1110
            ^^^

Bits as 0's:

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?


Related:

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mithrax
  • 7,603
  • 18
  • 55
  • 60
  • Your description of "first" bit is ambiguous. Are you referring to the Most Significant or Least Significant bits? – staticsan Apr 15 '09 at 04:00
  • From some of your comments it seems like you are having a hard time determining which mask number corresponds with which binary digits. Is that correct? – Steve Rowe Apr 15 '09 at 05:15
  • @Mithrax et al: I formatted the question and (hopefully) clarified it a little. – paxdiablo Apr 15 '09 at 06:18

7 Answers7

19

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

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

Example: Word of 0x0448.

Settings bits 1, 2 and 3 would be Word OR 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:

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

 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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
staticsan
  • 29,935
  • 4
  • 60
  • 73
  • 1
    @staticsan, I expanded with the bit operations to make it clearer, then +1'ed you since the answer is now so much better :-) Hope you don't mind. – paxdiablo Apr 15 '09 at 06:07
4

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

  • clear bits: x & (~m)
  • set bits: x | m
  • flip bits: 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

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

  • ~ is the 1-complement: ~10001010 = 01110101
  • & is the bitwise AND
  • | is the bitwise OR
  • ^ is the bitwise XOR
  • << is the left bit shift: 10001010 << 2 = 00101000
Varkhan
  • 16,601
  • 5
  • 31
  • 24
1

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

JP Alioto
  • 44,864
  • 6
  • 88
  • 112
  • Right, but what would my mask be? 0x0000? Do you see what I mean? When doing AND I need to have a mask, how do I specify zero's with just those positions as zero? – Mithrax Apr 15 '09 at 03:57
  • If you want to keep what you have there do AND with 1, if you want to erase what you have there, AND with 0. – JP Alioto Apr 15 '09 at 04:32
0

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

 1 <<p puts 1 at the position p, for example

 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 tilde to clear the other half.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Vlad
  • 4,425
  • 1
  • 30
  • 39
0

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

AND (NOT 0x14)

CookieOfFortune
  • 13,836
  • 8
  • 42
  • 58
0

For clearing the bits, use AND with 0x440.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chand
  • 87
  • 4
  • That would work with 0x0448 but not any other number - you need 0xfff1 to leave all the other bits untouched. – paxdiablo Apr 15 '09 at 06:11
0
number &= ~0xe
chaos
  • 122,029
  • 33
  • 303
  • 309