Codes: BCD, Gray and UTF-8

Three codes in one place: BCD addition and subtraction digit by digit with the +6 correction, conversion to Gray with its XORs and the table that changes a single bit, and any text in ASCII and UTF-8, with each character’s template and what comes out if someone reads it wrong. It runs in your browser, on your phone too.

How to use it

  1. Choose BCD, Gray or Text.
  2. In BCD, type two numbers and choose add or subtract; in Gray, a number (in decimal or with 0b) and the bits; in Text, whatever you like, with accents, symbols or emoji. Or pick an example: the microwave, the exam-style question, a subtraction that comes out negative, the dial or the message «Está listo».
  3. Press Step: each digit with its binary sum and its correction, each Gray bit with its XOR or each character with its UTF-8 template.

What it works out

BCD 8421 stores each decimal digit in 4 bits. When adding, if a digit goes above 9 (or there is a carry out of the 4 bits), 0110 is added to skip the 6 invalid patterns, and the 1 that comes out goes to the next digit. Subtracting is adding the ten’s complement: the sign is 0 + 1 + carry and that carry is discarded.

Gray keeps the first bit and each of the others is the XOR of two neighbouring binary bits, so two consecutive numbers differ in a single bit. UTF-8 stores ASCII in 1 byte and the rest in 2, 3 or 4, with the templates of the course notes. Checked against the topic 6 simulators and Python.

The rules it follows

  • Numbers of up to 8 digits in BCD; Gray of 3, 4, 5 or 8 bits.
  • In subtraction, a negative result comes out in ten’s complement and it explains how to read it.
  • Text is read character by character (emoji too, which are a single 4-byte character in UTF-8).

For the exam

The exam has BCD sums such as 86 + 59 and, sometimes, Gray or a character in UTF-8. Do it on paper and check it here; the challenge below gives you new operations.