##
Setting bits to 1

Figure 2: Truth Table for Bitwise-OR

To turn certain bits on, the bitwise-OR operation can be used, following the principle that *n* OR 1 = 1

and **n** OR 0 = n

. Therefore, to make sure a bit is on, `OR`

can be used with a `1`

. To leave a bit unchanged, `OR`

is used with a `0`

.

Example: Setting 1 on bit 5 and 3, the other bits unchanged.

To light on the lamp which is connected on PB2, the bit 2 of the Port B DATA register needs to be set to 1. The following expression shows how to set bit 2 to 1:

PB = PB | _BIT2;

PB |= _BIT2;

##
Clearing bits to 0

**Figure 4**: Truth Table for Bitwise-AND

From the above results you can find that when a bit is `AND`

ed with a `0`

, the result is always 0, i.e. `n AND 0 = 0`

. To leave the other bits as they were originally, they can be `AND`

ed with `1`

, since `n AND 1 = n`

.

Example: Clearing on bit 5 and 3, the other bits unchanged.

To turn off the lamp which is connected on PB2, the bit 2 of the Port B DATA register needs to be clear to 0. The following expression shows how to clear bit 2 to 0:

PB = PB & ~_BIT2;

PB &= ~_BIT2;

##
Toggling bit values

**Figure 6**: Truth Table for Bitwise-XOR

We just discussed how to turn bits on and turn bits off, but not both at once. Sometimes it does not really what the value is, but it must be made the opposite of what it currently is. In the other word, toggling means that turns the bit on if it is off, and turns it off if it is on. This can be implemented using the `XOR`

(exclusive or) operation. Therefore inversion of the values of bits is done by `XOR`

ing them with a `1`

. The other unchanged bits can be `XOR`

ed with `0`

because `n OXR 0 = n`

, just like an `OR`

.

Example: Inverting on bits 5 and 3, the other bits unchanged.

To toggle the lamp which is connected to PB2, the bit 2 of the Port B DATA register needs to be inverted. The following expression shows how to invert the bit 2:

PB = PB ^ _BIT2;

PB ^= _BIT2;

##
Set bits to a value

To write arbitrary 1s and 0s to a subset of bits: first, write 0s to that subset, then set the high bits.

Example: Set *register*[4:2] to (010)_{2}

register = **(register & ~(_BIT4 | _BIT3 | _BIT2))** | (_BIT3);

##
Checking bit value

So far, you already learn how to change the bit values. But now, suppose you want to determine the particular bit that is set or not. The following test does not necessarily work:

if ( PB == _BIT2) *// no good - testing bit 2*

That is because even if the corresponding bit in PB is set to 1, other bits might also be set to 1. The equality above is true *only* when the corresponding bit is 1.

The Bitwise-AND operator (&) can be used to check the bit state. First, use bitwise-AND to `AND`

**PB** with a bitmask value (The corresponding bit in bitmask must be set to 1, leaves other bits to 0). This produces a value that is 0 in all the other bit positions because `0 AND any value is 0`

. Only the bit corresponding to the 1 is left unchanged because `1 AND any value is that value`

. Therefore, if the result is zero, then the bit was off. But, if the result is any other value, then the bit was on. The proper test is this:

if ( (PB & _BIT2) == _BIT2) *// testing bit 2 == 1 ?*

For one-bit testing, programmers often simplify this test to the following:

if (PB & _BIT2) *// testing bit 2*

if ( (PB & _BIT2) == 0) *// testing bit 2 == 0 ?*

For one-bit testing, programmers often simplify this test to the following:

if ( !(PB & _BIT2)) *// testing bit 2 == 0 ?*

For multiple-bit testing, you need to identify the correct value to compare with the result: