Lessons and the learning game

Binary, hex and bits

What a number really is inside a computer, taught on an Uno with one lamp a bit.

How it works

Binary, hex and bits runs every lesson on the same circuit, the gallery's level meter: an Arduino Uno, a ten-segment LED bar graph on pins 2 to 11, and a knob on A0. Each sketch lights bit n on pin 11 - n, so the bar reads like a binary number written left to right. The knob is the input: the Uno's analog reading is a 10-bit number, exactly one lamp a bit.

Every lesson opens a working sketch in C. You run it, read what the serial monitor prints, then change a line and run again. The checks read the serial output and the lamps, so every number a lesson quotes is one the board really printed.

The lessons

  1. Counting in binary: a counter you can watch, what each lamp is worth, reading 0b101101 by hand, why ten bits stop at 1023, rolling over, and the knob as a 10-bit number.
  2. Hex shorthand: one hex digit is four bits. Printing with %X, a hex parser written by hand, and lighting any lamp you like by typing its hex.
  3. AND OR XOR: the bitwise operators on four-bit examples, then on the knob against a mask. AND keeps bits, OR forces them on, XOR flips them, and XOR twice gives back what you started with.
  4. Shifts and masks: << and >> multiply and divide by two, the four one-bit moves (set, clear, toggle, test), pulling a field out of a packed number, and a scanner eye that sweeps the bar.
  5. Signed numbers: two's complement on eight lamps. Why -1 is all ones, flip and add one, what happens at 127 + 1, one byte read two ways, and a submarine dive that does not fit an int8_t.
  6. Floats and their lies: ten dimes that miss a dollar by one bit, the sign, exponent and fraction of a float, a number too big to add one to, and comparing with a tolerance instead of ==.

The project: a binary clock reader

The last lesson packs a whole clock into the ten lamps: the hour in the left four bits and the minutes in the right six, put together with a shift and an OR and read back with a mask. The sketch ships with a mask bug that hides for half of every hour; you find it at 10:32 and fix it. Then a quiz shows times on the lamps and you type what you read. It ends with ideas to make it yours: seconds, a 24-hour clock that needs one bit more than you have, or your own quiz with a score.

What the Uno does differently

  • An int is 2 bytes on the Uno, not 4, and a double is the same 4-byte float as a float. Code moved from a laptop can quietly change meaning.
  • The ATmega328P has no divide instruction, so a divide by a power of two is much faster as a shift.
  • Serial.print(x, BIN) prints no leading zeros, so the sketches print bits with a small loop of their own.
  • On a real Uno, snprintf has no %f unless you link the bigger library, so floats are printed with Serial.print(value, digits).

After this course

Next is Pointers and memory in C, where the hex numbers you can now read turn out to be addresses.