1.4.1 - Bitwise operations, shifts and masks
A byte can store eight independent flags rather than one number. This part of 1.4.1 applies AND, OR and XOR to individual bits, builds masks to select or change flags, and follows left and right logical shifts. You will keep the bit width explicit and explain when a shift's numeric interpretation is limited by lost bits or signedness.
Operate on corresponding bits
A byte's operator can act separately on each position.
Bitwise operation
A bitwise operation applies a logical rule independently to each pair of corresponding bits.
The patterns must be aligned to the same width. This differs from applying a logical operator to the truth of the whole value.
| First bit | Second bit | AND | OR | XOR |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
AND produces 1 only when both inputs are 1. OR produces 1 when at least one input is 1. XOR, exclusive OR, produces 1 when the bits differ. For eight-bit patterns:
A 10110110
B 01101100
A AND B 00100100
A OR B 11111110
A XOR B 11011010
For example, at bit position 5 both inputs are 1, so AND and OR give 1 while XOR gives 0. At bit position 7 the inputs differ, so AND gives 0 while OR and XOR give 1. The operation determines a new pattern; whether that pattern later represents flags, an unsigned number or a signed number is a separate interpretation.
Use AND masks to select or clear bits
A mask is a pattern chosen to select positions. With AND, a mask bit 1 preserves the corresponding data bit because b AND 1 = b; a mask bit 0 clears it because b AND 0 = 0. This can extract selected flags or clear flags without disturbing the others.
Number bit positions from 0 at the right to 7 at the left. Suppose status 10110110 uses bit 2 for an alarm. Mask 00000100 selects that bit:
Status 10110110
Mask 00000100
AND 00000100
The nonzero result means the alarm bit was set. If the alarm were clear, the result would be 00000000. The result need not be the integer one: its selected position still has weight four if read as an unsigned integer. Compare with zero to test whether any selected bit is set; compare with the full mask to test whether every selected bit is set.
To clear bit 2 while preserving all other positions, use 11111011. Then 10110110 AND 11111011 = 10110010. To extract the low four bits, use 00001111; the same status becomes 00000110. If the selected field is not at the right, an additional logical right shift can move it there: extracting the high nibble with 11110000 gives 10110000, then shifting right four gives 00001011.
Use OR to set and XOR to toggle
With OR, a mask bit 1 forces that position to 1, while a mask bit 0 preserves the data. To set bits 4 and 0 in 10100110, use mask 00010001: the result is 10110111. An already-set bit remains set; OR does not reverse it.
With XOR, a mask bit 1 toggles the position: 0 becomes 1 and 1 becomes 0. A mask bit 0 preserves it. Applying the same 00010001 to 10100110 also happens to give 10110111, because both targeted bits were initially zero. Apply the mask again: 10110111 XOR 00010001 = 10100110. In contrast, a second OR leaves 10110111 unchanged. Identical results on one starting state do not make the operators interchangeable.
The required action determines the operator and mask.
AND with a mask 0 clears a position; OR with a mask 1 sets it; XOR with a mask 1 toggles it. Preserve the other positions with that operator's neutral mask bit.
For example, a command “enable the light” should set its flag even if it was already enabled; a toggle could turn it off.
Logical shifts move bits and insert zeros
A shift changes a pattern's positions within a declared width.
Logical shift
A logical shift moves every bit a stated number of positions within a fixed width. Vacated positions receive zeros; bits that leave the width are discarded.
An eight-bit logical left shift of 00110110 by two gives 11011000. An eight-bit logical right shift of the original by two gives 00001101, discarding the original low bits 10.
For unsigned values, a left shift by one multiplies by two only if no nonzero high bit is lost. Here 54 shifted left two becomes 216, exactly 54 × 4. But 11010000 (208) shifted left one becomes 10100000 (160): the mathematical 416 does not fit eight bits. The retained value is the mathematical product modulo 256, so a blanket “left shift always doubles” is false at fixed width.
Unsigned logical right shift by gives the integer quotient on division by , discarding the remainder. For 54 right two, , so the result is 13, not the fractional answer 13.5. If the discarded bits are all zero, the division is exact. A shift by at least the width clears every bit under this logical-shift convention.
Continue this lesson.
You’re previewing NovaLearn without an account. Create a free account to read the rest.
Create free account