The lesson
4.1 Logic
Outcome 1 of 12 in domain 4 · Digital and microcontrollers
Binary, hexadecimal, gates, truth tables.
Understand
The pack’s temperature sensor answers with two bytes on the I²C bus: 0xFF then 0x80. Read one way, that is −0.5 °C, the pack outside in November. Read another way, it is 255.5 °C. The bits are the same; what changes is the convention applied to them, and the sensor does not send it.
The oldest convention is binary. Leibniz publishes in 1703 his "Explanation of Binary Arithmetic", which counts with the characters 0 and 1 alone. He has no machine in mind; through the letters of the Jesuit Bouvet, he recognizes in it the hexagrams of the I Ching. In 1854, George Boole publishes The Laws of Thought, an algebra where variables are only 0 or 1; Jevons, ten years later, gives it the rule we use, x + x = x, the "or" of two truths is a truth. Peirce sees as early as 1886 that an electrical circuit could compute it. Nobody does for fifty years.
Then a master’s student, Claude Shannon, at MIT in 1937, and Nakashima in Japan and Shestakov in the USSR each on their own, notice that the relays of a telephone exchange obey this algebra: two contacts in series make an AND, two in parallel make an OR, a contact at rest makes a NOT. The gates of this RAS are those contacts, etched in silicon.
Many computers of the 1950s have words of 36 bits, which cut into threes, hence octal. In 1964, IBM announces the System/360 and fixes addressing on the eight-bit byte. Eight does not cut into threes; it cuts into two nibbles, and each nibble is written with a single digit, 0 to F. Hexadecimal is binary’s shorthand, four bits at a time: 0xFF, two full nibbles, 255.
An odometer running backwards goes from 000000 to 999999; a byte that backs up from zero shows 0xFF. That is two’s complement, which von Neumann proposes in 1945: the most significant bit, on the left, weighs −128 instead of +128. So 0xFF is 255 read unsigned and −1 read signed; 0x80 is 128 or −128. On 12 signed bits, the count runs from −2048 to 2047. The TMP102 delivers its temperature on 12 signed bits, 0.0625 °C per step: −0.5 °C is −8 steps, 0xFF8, left-justified in two bytes, 0xFF 0x80. Read unsigned, 4088 steps, 255.5 °C.
Two other conventions live in the same eight bits. BCD, where each nibble is a decimal digit: the seconds register of a DS3231 clock says 0x59 for 59 seconds, because the display reads in decimal; in binary, 0x59 would be 89. ASCII, standardized in 1963, where 0x41 is the letter A and 0x30 the character "0", which is not zero. Three readings of 0x59: 89, 59, or the letter Y.
In code, bits are touched one at a time with a mask. PORTB |= (1 << 3) sets bit 3 to 1 and leaves the other seven; PORTB &= ~(1 << 3) clears it, the other seven untouched; (x >> 4) & 0x0F brings the high nibble, bits 7 to 4, down to the place of bits 3 to 0, and cuts the rest. A mask says which bits it touches; it is enough to write it in binary.
The classic mistake has two faces. The sign misread: −0.5 °C becomes 255.5 °C. And the two bytes in the wrong order: the TMP102 sends the most significant byte first, a BMS that speaks SMBus sends the least significant first, and a pack at 25 °C, 0x19 0x00, read backwards gives 0x0019, that is 0.06 °C. Danny Cohen named this quarrel in 1980, Big-Endians and Little-Endians, after the war of the egg in Swift.
What the source lacks
Masks in code. Kuphaldt writes for gates in packages; on a bare chip, the gates are in the instructions, and |=, &= ~ and >> touch a register without disturbing its neighbours.
Sensor registers over I²C. The TMP102 on 12 signed bits, the Texas Instruments LM75 on 9 bits in 0.5 °C steps (NXP’s has 11), the clock in BCD, the BMS that delivers the pack voltage in millivolts over two bytes: each has its convention, and only the datasheet says which.
Byte order. Nothing in Kuphaldt; it is the first thing to check when two bytes make a number.
See
To come: the wheel of the 256 values of a byte, from 0x00 to 0xFF, with two scales side by side, unsigned from 0 to 255 and two’s complement from −128 to 127, and the notch where the counter backs up from zero to 0xFF. Below, one byte, 0x59, read three times: binary, BCD, ASCII.
The targets
- Convert and read an address. Sixteen conversions without a slip between decimal, binary and hexadecimal, and the address of a TMP102’s configuration register,
0x01, and of the chip on the bus,0x48, in all three bases. - The gates and the XOR. The six truth tables from memory, then an exclusive OR built from four NANDs of a 74HC00 and checked line by line.
- The mask. Three lines of code given; for each, the mask written in binary and the bits it sets, clears or reads.
- The most significant bit. Why it weighs −128 in a signed 8-bit integer;
0x80and0xFFread both ways. - Three conventions, three bytes. A TMP102 temperature in two’s complement, a DS3231’s seconds in BCD, a character received on the UART in ASCII, by hand, then in the program.
- Diagnosis. A sensor that returns 255.5 °C or 0.06 °C: the two hypotheses, the sign and the byte order, and the re-reading that decides.
Misconceptions
"Binary is another kind of number."
Kuphaldt, chapter 2: a number is a number, whatever its spelling.0xFF, 255 and11111111are the same number. What changes the value is the signed or unsigned reading, not the base."The sign bit is a flag; remove it and read the rest."
That is sign-and-magnitude notation, which processors do not use. In two’s complement, the left bit has a weight, −128, which adds to the others:0xFFmakes −128 + 127 = −1, not −127."An overflow shows."
127 + 1 makes −128 in a signed byte, with no error and no message; the counter goes around in silence. Kuphaldt shows it in "Overflow".
The word
Bit
The word is John Tukey’s, a statistician at Bell Labs, in a memo of 9 January 1947: he contracts binary digit, and the English word also means "small piece". Claude Shannon puts it in print in 1948, in "A Mathematical Theory of Communication", crediting Tukey. French proposed "élément binaire"; usage kept bit. In French, bit, the same word.
Read
Kuphaldt, Lessons in Electric Circuits, volume IV (Digital), chapter 1: Numeration systems, "Decimal versus binary numeration" and "Octal and hexadecimal numeration", why hexadecimal won over octal. Chapter 2: Binary arithmetic, "Negative binary numbers", "Overflow", and "Bit groupings" for the nibble. Chapter 3: Logic gates, "The NAND gate", "The Exclusive-OR gate" and "Gate universality". Chapter 7: Boolean algebra, "Introduction", which tells Boole and Shannon, and "Boolean arithmetic", where 1 + 1 = 1. He dates Shannon’s thesis to 1938, its publication; it was written in 1937.
Bridges
- History
- Leibniz in 1703, Boole in 1854, Shannon in 1937: three ideas without a machine, then one machine that takes them all, A Symbolic Analysis of Relay and Switching Circuits.
- Mathematics
- A base is a choice of writing; binary is the shortest in symbols and the longest in digits, Binary number.
- Philosophy
- Boole wanted to write the laws of the mind as equations; what he wrote became the blueprint of circuits, The Laws of Thought.
- Literature
- The Big-Endians and Little-Endians of Lilliput fought over an egg; ours, over the order of two bytes, Endianness.
What it unlocks
What next?
- 4.2 Logic circuits: the flip-flop keeps a bit from one clock tick to the next, and the counter makes numbers of them.
- 4.3 Interfaces: how the TMP102’s bytes travel over I²C, and why the bus wants pull-up resistors.
- 1.2 The quantities: prefixes by thousands; here the kilo is 1,024, and it is the only time.
- A question to be written, "The sensor that reads 255.5 °C": two bytes, two hypotheses, one re-reading.