levo

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.

Your recallNot started
Key term

What is the effect of XOR with 11111111, and of XOR with 00000000?

Key term

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. 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. 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. 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. 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. 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. 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.

1Closed book
2Open book
3Closed book

The 3-step method, run for you

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz makes facts automatic

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It gets the exam shape right

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

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.