OCR H446, section 1.4.1 (i)
A processor can work on individual bits inside a value, using bitwise operations. They are fast, and are used in graphics, encryption, networking and hardware control.
Shifts
A shift moves all the bits of a value left or right by a number of places.
Logical shift
In a logical shift, the bits that move out are lost, and the places that open up are filled with 0.
Logical shift left by 1:
Logical shift right by 1:
Effect on the value
- Shifting left by 1 multiplies by 2, because every bit becomes worth twice as much.
- Shifting right by 1 divides by 2 (rounding down, since the lowest bit is lost).
- Shifting by places multiplies or divides by .
| Operation | Result |
|---|---|
| () | (12) |
| () | (5) |
Beware: if a 1 is shifted out of the left end, the value is no longer correct (an overflow). If a 1 is shifted out of the right, it is a remainder that is lost.
Arithmetic shift
For two's complement numbers, a logical right shift would put a 0 in the sign bit, and make a negative number positive. An arithmetic shift right copies the sign bit into the empty place, so the sign is kept.
Bitwise logic
The operations AND, OR, XOR (and NOT) are applied to each pair of bits in the same position.
| A | B | A AND B | A OR B | A XOR B |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
10110110 10110110 10110110
AND 00001111 OR 00001111 XOR 00001111
-------- -------- --------
00000110 10111111 10111001
Masks
A mask is a binary pattern that is combined with a value, to select or change particular bits. Each mask uses a different operation.
AND with a mask: select (keep) or clear bits
AND keeps a bit where the mask has 1, and clears it to 0 where the mask has 0.
Keep only the lower 4 bits (mask ):
10110110
AND 00001111
--------
00000110
Test whether a particular bit is set: AND with a mask that has a 1 only in that position. If the result is non-zero, the bit was 1.
10110110 Is bit 4 set? (value 16)
AND 00010000
--------
00010000 -> non-zero, so yes
Convert a lower case letter to upper case in ASCII: a lower case letter differs from the capital by bit 5 (value 32). Clearing it with the mask :
01100001 ('a' = 97)
AND 11011111
--------
01000001 ('A' = 65)
OR with a mask: set bits
OR sets a bit to 1 where the mask has 1, and leaves the others alone.
Set bit 0 and bit 1:
10110100
OR 00000011
--------
10110111
XOR with a mask: toggle (flip) bits
XOR flips a bit where the mask has 1, and leaves the others alone.
10110110
XOR 11110000
--------
01000110 the top four bits are flipped
Applying the same XOR twice gives back the original value, which is why XOR is used in simple encryption (the Vernam cipher) and for swapping or toggling.
NOT
NOT inverts every bit: .
Summary of masks
| Goal | Operation | Mask |
|---|---|---|
| Keep some bits, clear the others | AND | 1s where you want to keep |
| Test a bit | AND | A single 1 in that position |
| Set bits to 1 | OR | 1s where you want to set |
| Clear bits to 0 | AND | 0s where you want to clear |
| Toggle bits | XOR | 1s where you want to flip |
Uses
- Reading and setting flags: a single byte can hold 8 yes or no settings.
- Extracting parts of a value: for example the red, green and blue parts of a colour.
- Network addresses: a subnet mask is ANDed with an IP address to find the network part.
- Fast multiplication and division by powers of 2, using shifts.
- Encryption, and checksums.
- Controlling hardware, where each bit of a register controls something.
Worked example
Task: a byte status is . (a) Extract the upper 4 bits. (b) Set bit 2. (c) Toggle bit 7. (d) Multiply by 4.
- (a) AND with : . (Shift right 4 to read it as , which is 11.)
- (b) OR with : .
- (c) XOR with : .
- (d) Shift left 2: (the top two bits are shifted out, so for this value overflow has happened, and the answer isn't ).
Exam tip: know which operation does which job with a mask: AND to keep, clear or test bits, OR to set bits, and XOR to toggle them. For shifts, state logical or arithmetic, give the effect (× or ÷ by 2 each place), and watch for lost bits.
Check yourself
What is the result of a logical shift left by 3 on 00000101?
00101000 (5 × 8 = 40).
What mask and operation would you use to clear the top four bits of a byte?
AND with 00001111.
Calculate 01101010 XOR 11111111
10010101. XOR with all 1s flips every bit.
How can you test whether the least significant bit of a number is 1?
AND the number with 00000001. If the result is not zero, the bit is 1 (so the number is odd).