Skip to content

Switch by Switch

Four tools that work a byte one switch at a time, to keep some bits, force others on, and flip the ones you choose.

So far we have read bytes. Now we start changing them.

Not the number as a whole, but the switches inside it, one position at a time. There are four tools for the job, and each one fits in a four-line table. Line two bytes up and these work straight down the columns, pairing the bits in the same place.

Two jobs, one set of names

You have met AND, OR and NOT already. They joined questions together, and each one gave back a single yes or no.

These four do a different job. They take two numbers, work down the columns a bit at a time, and give a number back.

Same idea, different scale, so we spell them apart: BITAND, BITOR, BITXOR and BITNOT. Some real languages reuse the plain words for both jobs, which is a trap worth remembering, because the two behave nothing alike.

AND keeps only where both are on

BITAND looks at one bit from each byte and gives back a 1 only if both are 1:

A B A BITAND B
0 0 0
0 1 0
1 0 0
1 1 1

Its great use is masking: keeping some switches and forcing the rest off. Put a 1 in the mask everywhere you want to keep a bit, and a 0 everywhere you want it gone.

  11001010     the byte
AND 00001111   the mask: keep the low four, drop the high four
= 00001010     only the low nibble survives

The survivors spell an ordinary number:

SHOW BIN 11001010 BITAND BIN 00001111
Output
10

OR turns switches on

BITOR gives back a 1 if either bit is 1, so it sets bits. It forces chosen switches on and leaves the rest alone.

A B A BITOR B
0 0 0
0 1 1
1 0 1
1 1 1
  00001001     the byte
OR  00000010   set bit 1
= 00001011     bit 1 is now on, the others are untouched
SHOW BIN 00001001 BITOR BIN 00000010
Output
11

Bit 1 is on and everything else stayed put. BITOR only ever turns switches on.

XOR flips the ones you choose

BITXOR gives back a 1 only when the two bits differ. Put a 1 in the mask and that switch flips. Put a 0 and it is left alone.

A B A BITXOR B
0 0 0
0 1 1
1 0 1
1 1 0
  11111111     the byte
XOR 00001111   flip the low four
= 11110000     low nibble inverted, high nibble untouched
SHOW BIN 11111111 BITXOR BIN 00001111
Output
240

Apply the same mask a second time and it flips straight back. BITXOR is the toggle that undoes itself.

NOT flips the lot

BITNOT flips every bit:

SHOW BITNOT BIN 00001111
Output
240

00001111 became 11110000, which is 240. That is the same flip BITXOR just did with an all-ones mask.

BITNOT needs one thing the others do not: it has to know how wide the number is. “Flip every bit” means something different in a byte than in a sixteen-bit value, because there are more bits to flip. Here we work a byte at a time, so BITNOT flips eight.

When it’s wrong, see why

  • You reached for AND and got a yes or no, not a mask. That is the question-joining AND from Decisions. Masking wants BITAND.
  • A mask did the opposite of what you wanted. Match the tool to the job. BITAND with a 0 clears, BITOR with a 1 sets, BITXOR with a 1 flips.
  • The whole byte changed when you meant to touch one bit. Check your mask. A 1 in the wrong column acts on a switch you did not mean to.

What you’ve learnt

  • Four tools work a byte one switch at a time.
  • BITAND masks, BITOR sets, BITXOR flips, BITNOT inverts the lot.
  • A mask is a byte whose 1s mark the switches you mean to act on.
  • These are not the question-joining AND, OR and NOT. They take numbers and give a number back.

What’s next

These tools change which switches are on. In Unit 2 we do something different, and slide the whole row sideways.