1

For example, if I want to set a bit in y at position n (in C)

y = y | (1 << n)

But if I want to delete a bit in y at position n I have to use the ~ operator after binary AND.

y = y & ~(1 << n);

My question: Why Must I use the ~ operator?
Is this because the result turns into negative area?

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
  • There are no "negative" number, just bits. Take a piece of paper and a pencil and do these operations by hand. Hello De Morgan. – Jabberwocky Feb 14 '18 at 18:10
  • Because to set a bit you need to OR the value with a mask having a single bit set. To reset ("delete") a bit you must AND the value with a mask having all bits set *except* that bit. The `~` inverts the single-bit mask to do the AND. – Weather Vane Feb 14 '18 at 18:10
  • "Why Must I set the ~ operator?" No, `~` is one way. `y = (y |(1 << n)) ^ (1 << n);` is another. Many ways to solve this. Unless `1` meets the width of `y`, it is the wrong approach. – chux - Reinstate Monica Feb 14 '18 at 18:29

4 Answers4

4

If you want to set a bit at third place from the right :

Y :            01001000
1 << 2 :       00000100
Y | (1 << 2) : 01001100 The | is OR, bits are set to 1 if any is 1.

If you want to remove the bit :

1 << 2 :         00000100
~(1 << 2) :      11111011  The ~ is NOT, bits are inversed
Y :              01001100
Y & ~(1 << 2) :  01001000 The & is AND, bits are set to 1 if both are 1.

I suggest you read more about Bitwise operators

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
1

No, ~ has nothing to do with interpreting the number as negative: tilde ~ operator interprets the number as a pattern of bits, which it then inverts (i.e. replaces zeros with ones and ones with zeros). In fact, if you apply ~ to an unsigned value, the result would remain positive.

Recall that 1 << k expression produces a pattern of all zeros and a single 1 at the position designated by k. This is a bit mask that can be used to force bit at position k to 1 by applying OR operation.

Now consider what happens when you apply ~ to it: all 0s would become 1s, and the only 1 would become zero. Hence, the result is a bit mask suitable for forcing a single bit to zero by applying AND operation.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
0

The ~ operator turns all of the 0's to 1's and all of the 1's to 0's. In order to clear the bint in position n you want to and it will all ones and a zero in the nth position so shift a one to the nth position and ~ invert all the bits.

cleblanc
  • 3,678
  • 1
  • 13
  • 16
0

1 << n for n==3 (just an example) gives you a pattern 0000000...0001000. ~ negates the bit pattern to 11111111....11110111. Using the bitwise AND operator (&) will only set the required bit to 0, all other remain with the same value. It's using the fact that for a bit b: b & 1 == b.

~ flips all bits, it has nothing to do with negative numbers.

A graphical representation for a sequence of k-bits

pos        k-1 k-2                           0
          +---+---+-------------------+---+---+
1:        | 0 | 0 |        ···        | 0 | 1 |
          +---+---+-------------------+---+---+

pos        k-1 k-2        n   n-1           0  
          +---+---+-----+---+---+---+-----+---+
1<<n      | 0 | 0 | ··· | 1 | 0 | 0 | ··· | 0 |
          +---+---+-----+---+---+---+-----+---+


pos           k-1 k-2     n   n-1           0  
          +---+---+-----+---+---+---+-----+---+
~(1<<n)   | 1 | 1 | ··· | 0 | 1 | 1 | ··· | 1 |
          +---+---+-----+---+---+---+-----+---+
Pablo
  • 13,271
  • 4
  • 39
  • 59