Skip to content
Game 0 Unit 13 of 16 1 hr learning time

Working With Bits

Change one bit without disturbing the rest. Meet ora, and and eor — set, clear and flip individual bits — the everyday way to throw a single switch in a byte.

81% of Meet The Machine

Back in The Machine Can Hear You you tested a single bit by masking it. Often you want to change one — set a flag, clear a flag, flip a flag — without touching the seven bits around it. Three instructions do exactly that, one bit at a time:

  • ora n — OR with A: set every bit that's 1 in n, leave the rest alone.
  • and nkeep every bit that's 1 in n, clear the rest.
  • eor n — Exclusive-OR: flip every bit that's 1 in n. (On the 6510, XOR is spelt eor.)

A byte is often not one number but eight little switches — the joystick port from Unit 8 was exactly that, five direction bits in one byte. These three instructions are how you throw one switch and leave the others where they were.

What you'll see by the end

The C64 screen surrounded by a cyan border.
Red (2) became cyan (3) by ORing in the bottom bit — one switch thrown, the rest left alone.

A cyan border — colour 3. We started with colour 2 (red), ora'd in bit 0, and got 3. In bit terms it's "set the bottom bit and leave the rest"; in colour terms, red became cyan. One switch thrown.

Setting a bit

; Meet the Machine - Unit 13: Working With Bits
; Assemble with: acme -f cbm -o bits.prg bits.asm

*= $0801
!byte $0c,$08,$0a,$00,$9e,$32,$30,$36,$31,$00,$00,$00

*= $080d
        lda #%00000010  ; colour 2 — red
        ora #%00000001  ; SET bit 0, leave the rest alone
        sta $d020       ; %00000011 = 3 = cyan
loop    jmp loop

%00000010 is colour 2. ora #%00000001 sets bit 0 without disturbing bit 1, giving %00000011 = 3. The power of ora is that it's surgical: it only ever turns bits on, so whatever you already had survives.

Assemble and run

acme -f cbm -o bits.prg bits.asm

Red, with one bit set, becomes cyan.

Try this: clear a bit with and

Start from lda #%00000111 (colour 7, yellow) and and #%00000110. The and keeps only the bits that are 1 in the mask — so bit 0 is cleared and you're left with %00000110 = 6 (blue). and is the surgical off switch, the mirror of ora.

Try this: flip a bit with eor

Take any value and eor #%00000111. Each of the bottom three bits flips — on becomes off, off becomes on. Run it twice on the same value and you're back where you started: flipping twice undoes itself. (That reversibility is exactly why eor is the heart of sprite drawing — a topic for later.)

If it doesn't work

  • ora/and/eor won't assemble. They take an immediate mask: ora #%00000001. The # matters — without it you'd OR with the contents of an address, not the bits you wrote.
  • The colour didn't change as expected. Work the mask bit by bit against the starting value: ora turns bits on, and keeps the masked ones, eor flips. Sketch the eight bits if you have to.
  • You set bits above bit 3 and the border looks unchanged. The border only shows the low four bits; higher bits are still set (check the register view) — they just don't colour the border.

What you've learnt

ora sets bits, and clears them, eor flips them — so you can change one bit of a byte and leave the other seven untouched.

What's next

A byte tops out at 255 — too small for a screen address, which runs to 65535. Next — Bigger Than a Byte — we hold a number in two bytes and add it by hand, and the carry flag from Unit 12 turns out to be the bridge that makes it work.