levo

Data Types and Number Representation

Masks with AND, OR and XOR

  • 2 things to know
  • Component 01
Start revising · £5/month

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.

Everything in your subscription

Runs, plus a mode for every other kind of session.

1Flash
2Imposter
3Fade
4Brain dump

Learn every subtopic in runs of four questions

Facts flash and fade, a wrong word hides in some of them, and a brain dump decides which questions are locked in.

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

Cram the night before, weakest topic first

A quick scan finds your weakest topics. Recall each fact you missed three times in a row.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz consolidates the facts

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

✓What it iscovered
~Why it happenspartly there
How long it lastsnot there yet

Explain It tests your understanding

Explain a concept in your own words and see which of its points you covered.

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

Test It catches misconceptions

Spot the fault in a statement. The faults are the mistakes students make most often.

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.

Learn it in runs of four questions, each ending on a brain dump against the clock. Every question you lock in comes back the next day, then at longer gaps, each time just before you would forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.