OCR A-Level Computer Science H446
Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation
Software Development
Types of Programming Language
Exchanging Data
Networks
Web Technologies
Data Types and Number Representation
Data Structures
Boolean Algebra
Legal, Moral, Cultural and Ethical Issues
Elements of Computational Thinking
Problem Solving and Programming
Algorithms
Data Types and Number Representation
Masks with AND, OR and XOR
- 2 things to know
- Component 01
Overview
Recall questions on bit masks, using AND to clear, isolate and test bits, OR to set bits, XOR to toggle bits and in simple encryption, testing for even numbers, packed settings, and converting letter case with a mask.
What you need to know
Answer each one before you reveal it.
The plaintext byte 01101000 is XORed with the key 00110011. Show the ciphertext, and show that XORing again with the key recovers the original.
Questions from this subtopic
Each one with the model answer underneath.
- 1
What is a bit mask?
Model answer
A binary pattern chosen so that combining it with a data value using a bitwise operator affects only the bits you are interested in and leaves the rest alone. The operator is applied to each pair of bits independently, with no carrying between columns, so the mask decides column by column what happens.
- 2
What does a bitwise AND with a mask do, bit by bit?
Model answer
Wherever the mask has a 1 the data bit passes through unchanged, because 1 AND 1 is 1 and 0 AND 1 is 0. Wherever the mask has a 0 the result is forced to 0, because anything AND 0 is 0. So AND either keeps a bit or clears it.
- 3
Work out 10110110 AND 00001111 and say what the mask has achieved.
Model answer
The result is 00000110. The top four mask bits are 0, so the top four data bits are cleared, and the bottom four mask bits are 1, so the bottom four data bits, 0110, survive. The mask has isolated the lower nibble, leaving 6.
- 4
How would you use an AND mask to test whether bit 2 of a byte is set, counting the rightmost bit as bit 0?
Model answer
AND the byte with 00000100, a mask with a single 1 in the bit 2 position. Every other bit is cleared, so the result is 00000100 if that bit was set and 00000000 if it was not. Then test whether the result is non-zero. For example 10110110 AND 00000100 gives 00000100, so bit 2 is set.
- 5
How do you test whether an integer is even using a mask?
Model answer
AND it with 00000001. That clears everything except the units bit, which is 1 exactly when the number is odd, because the units bit is the only one with an odd place value. If the result is zero the number is even. It is the same answer as taking the number modulo 2, but a single bitwise instruction rather than a division.
- 6
What does a bitwise OR with a mask do, bit by bit?
Model answer
Wherever the mask has a 1 the result is forced to 1, because anything OR 1 is 1. Wherever the mask has a 0 the data bit passes through unchanged, because 0 OR 0 is 0 and 1 OR 0 is 1. So OR either sets a bit or leaves it alone, which makes it the tool for turning flags on.
Other ways to work this
The same content, four other ways.
What is inside Levo
Six ways to work the same material.
Every question in Data Types and Number Representation, on a schedule.
Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.
Or £46 a year, about £3.83 a month. Cancel anytime.