Bitwise Manipulation - Masks
Computer Science: OCR A Level, topic 4.1.19
Bitwise Manipulation - Masks
Bitwise manipulation is the act of modifying data by changing the bits within individual bytes.

Masks
- Masks can be used with binary numbers to extract, set or toggle certain bits within a byte.
- The following examples use the mask 1 1 1 1 0 0 0 0.

AND
- The AND operation extracts whichever bits of the byte correspond to the 1s in the mask. The bits that correspond to 0 are returned as 0.
- For example, our AND mask extracts just the first four bits of a byte:
- Original byte: 1 0 1 1 1 0 0 1
- AND Mask: 1 1 1 1 0 0 0 0
- Result: 1 0 1 1 0 0 0 0

OR
- The OR operation sets digits equal to 1 (for bits corresponding to the 1s in the mask) or leaves them unchanged (for bits corresponding to the 0s in the mask).
- For example, our OR mask sets the first four bits of a byte to ‘1’:
- Original byte: 1 0 1 1 1 0 0 1
- OR Mask: 1 1 1 1 0 0 0 0
- Result: 1 1 1 1 1 0 0 1

XOR
- The XOR operation toggles binary digits (for bits corresponding to the 1s in the mask) or leave them unchanged (for bits corresponding to the 0s in the mask).
- For example, our XOR mask toggles the first four bits of a byte:
- Original byte: 1 0 1 1 1 0 0 1
- XOR Mask: 1 1 1 1 0 0 0 0
- Result: 0 1 0 0 1 0 0 1
Test yourself on Bitwise Manipulation - Masks
What is the result of the following operation?
10101010AND
01110001What is the result of the following operation?
10110001 XOR 01110010
What is the result of the following operation?
10101110 OR 11110111