bitGateLab Digital · Learn
English
Open the simulator

Learn › Topic 6

Number systems: binary, octal, hexadecimal and codes

7 min read · bitGateLab Team

A digital circuit understands only zeros and ones, but you need to represent quantities, signs, the digits of a display and the positions of an encoder. Here you will learn how positional systems work, how to convert between decimal, binary, octal and hexadecimal, how to represent negative numbers in 2's complement and how to use two codes that are very common in electronics: BCD and Gray.

Positional systems

In a positional system with base b, each digit is multiplied by a power of b according to its position, counting from 0 at the right. Decimal 347 is 3·102 + 4·101 + 7·100. The same happens with the other bases:

To avoid confusion, the base is indicated with a subscript (10102, 178, 3F16) or with a prefix such as 0b or 0x.

Converting between bases

From any base to decimal

Multiply each digit by its weight and add. For example, 2F316 = 2·256 + 15·16 + 3·1 = 512 + 240 + 3 = 755.

From decimal to binary

Divide by 2 repeatedly and write down the remainders; the binary number is read from the bottom up (the last remainder is the most significant bit). Another way is to subtract the largest power of 2 that fits, over and over.

Between binary, octal and hexadecimal

Since 8 = 23 and 16 = 24, there is no need to go through decimal: group the bits in sets of 3 (octal) or in sets of 4 (hexadecimal) starting from the right, and pad with zeros on the left if necessary.

Example 1: converting 156 to binary, octal and hexadecimal.

  1. Successive divisions: 156/2 = 78 r 0; 78/2 = 39 r 0; 39/2 = 19 r 1; 19/2 = 9 r 1; 9/2 = 4 r 1; 4/2 = 2 r 0; 2/2 = 1 r 0; 1/2 = 0 r 1.
  2. Reading the remainders from the bottom up: 156 = 100111002. Check: 128 + 16 + 8 + 4 = 156.
  3. Octal: 10 011 100 → 010 011 100 → 2 3 4. Result 2348 (2·64 + 3·8 + 4 = 156).
  4. Hexadecimal: 1001 1100 → 9 C. Result 9C16 (9·16 + 12 = 156).

Negative numbers: 2's complement

An n-bit register has no place for the "minus" sign, so a convention is used. The one used by practically every processor is 2's complement: the most significant bit has a negative weight, −2n−1, and the others keep their positive weight. With 8 bits the range runs from −128 to +127; with 4 bits, from −8 to +7.

To obtain the negative of a number:

  1. Write the positive number with all its bits.
  2. Invert every bit (this is the 1's complement).
  3. Add 1.

The advantage is huge: addition and subtraction are done with the same adder circuit, with no special logic for the sign. There is a single zero (00000000), and the most significant bit tells you directly whether the number is negative.

Example 2: representing −45 in 8 bits and computing 25 + (−12).

  1. 45 = 00101101. Inverting: 11010010. Adding 1: 11010011 = −45.
  2. Check with the weights: −128 + 64 + 16 + 2 + 1 = −45.
  3. Now 25 + (−12). 25 = 00011001; 12 = 00001100, inverted 11110011, plus 1: 11110100 = −12.
  4. Sum: 00011001 + 11110100 = 1 00001101. The carry out of bit 8 is discarded.
  5. Result: 00001101 = 13. Correct.
Overflow: if you add two numbers with the same sign and the result has the opposite sign, the result does not fit in the available bits. For example, in 8 bits 100 + 50 = 01100100 + 00110010 = 10010110, which in 2's complement is −106 instead of 150. Adding numbers with different signs never overflows.

BCD code

In BCD (binary-coded decimal, 8421 version) each decimal digit is represented separately with 4 bits. So 395 is written 0011 1001 0101. This is not the same as converting 395 to binary (1100010112): BCD uses more bits, but each group goes straight to a 7-segment display decoder, with no divisions. The codes 1010 to 1111 are not valid in BCD; in a design, those combinations are usually treated as don't-care conditions on Karnaugh maps.

Gray code

In Gray code, only one bit changes between one value and the next. In ordinary binary, going from 7 (0111) to 8 (1000) changes all four bits at once; if a sensor reads right in the middle of the transition, it may briefly see any intermediate value. With Gray code that error cannot happen, which is why it is used in position encoders, in counters that cross between different clocks and in the headings of Karnaugh maps.

DecimalBinaryGrayDecimalBinaryGray
000000000810001100
100010001910011101
2001000111010101111
3001100101110111110
4010001101211001010
5010101111311011011
6011001011411101001
7011101001511111000

Example 3: binary 1011 to Gray and back.

  1. Copy the first bit: 1.
  2. 1 ⊕ 0 = 1; 0 ⊕ 1 = 1; 1 ⊕ 1 = 0. Gray = 1110.
  3. Back again: copy the first bit, b3 = 1. Then b2 = 1 ⊕ 1 = 0, b1 = 0 ⊕ 1 = 1, b0 = 1 ⊕ 0 = 1. Binary = 1011.

In the table, decimal 11 appears with Gray 1110: it matches.

Common mistakes

Try it in the simulator

You can watch these representations "live" with a few inputs and outputs, and check the conversion to Gray code with XOR gates.

  1. Place four inputs as bits b3 to b0 and connect a display or four LEDs to see the value.
  2. Add three XOR gates: g2 = b3 ⊕ b2, g1 = b2 ⊕ b1, g0 = b1 ⊕ b0, and wire b3 straight to g3.
  3. Step the inputs from 0000 to 1111 and watch on the outputs that only one Gray bit changes between one value and the next.
  4. Generate the truth table of the circuit and compare it with the table in this article.

Exercises

  1. Convert 110101102 to hexadecimal and to decimal.
    Answer

    1101 0110 = D616 = 13·16 + 6 = 214.

  2. Convert 200 to binary and to hexadecimal.
    Answer

    200 = 128 + 64 + 8 = 110010002 = C816.

  3. Represent −100 in 8-bit 2's complement.
    Answer

    100 = 01100100; inverted 10011011; plus 1: 10011100. Check: −128 + 16 + 8 + 4 = −100.

  4. Convert decimal 6 to 4-bit Gray code and the BCD value 0111 0010 to decimal.
    Answer

    6 = 0110; Gray: 0, 0 ⊕ 1 = 1, 1 ⊕ 1 = 0, 1 ⊕ 0 = 1 → 0101. The BCD value 0111 0010 is 72.

To see how a circuit adds and subtracts these numbers, continue with binary adders.