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.

  • Foundations

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 work on the bits inside numbers and give a number back. BITAND, BITOR and BITXOR take two numbers and pair their bits. BITNOT takes one number and flips its bits.

Same idea, different scale, so we spell them apart: BITAND, BITOR, BITXOR and BITNOT. Real languages have their own rules: do not assume that AND means a bit mask just because its name looks familiar. In Spectrum BASIC, for example, 202 AND 15 gives 202, not the masked result 10 shown below. Its AND chooses a value according to whether the second value is zero; it does not pair the bits.

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.

Code
  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:

Code
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
Code
  00001001     the byte
OR  00000010   set bit 1
= 00001011     bit 1 is now on, the others are untouched
Code
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
Code
  11111111     the byte
XOR 00001111   flip the low four
= 11110000     low nibble inverted, high nibble untouched
Code
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:

Code
SHOW BITNOT BIN 00001111
Output
240

00001111 became 11110000, which is 240. You could also flip all eight bits with BIN 00001111 BITXOR BIN 11111111: the all-ones mask flips every position.

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.

Start with 11001010. What does XOR with 00001111 change? What if you apply it twice?

Only the low four bits flip: 11001010 becomes 11000101 (197). The high four stay 1100. Apply the same mask again and the original 11001010 (202) returns. A 0 in an XOR mask leaves that position alone.

For a picture made from the same eight bits, try Meet Assembly’s byte-and-pixels experiment. Its starting program explains how a stored byte becomes a row of pixels. BASIC is not a prerequisite.

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.

Sources

Zilog’s Z80 CPU User Manual, UM008011-0816, instruction entries AND, OR, XOR and CPL, describes the machine operations. The Amstrad ZX Spectrum +3 User Manual, pp. 99–101, explains BASIC’s conditional AND, OR and NOT rules.