C72 of 57
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

C7 · Lesson 2 · Binary, hex and two's complement

Writing numbers the way hardware stores them

A digital circuit stores numbers as rows of bits: wires and flip-flops that are each 0 or 1. Eight of them make a byte, which can mean 0 to 255, or −128 to +127, depending only on how you choose to read it. Engineers write bit patterns in hexadecimal, where each digit stands for four bits, so a whole byte fits in two characters. And negative numbers use two's complement, which lets the same adder circuit subtract.

Play

  • The dots are the eight bits of a byte, most significant on the left. Slide n to set its value.
  • Watch the readouts as you cross 127 to 128. What happens to the value read as a signed number?
  • Find a value whose hex is all letters. How many bits does each hex digit cover?

Predict

In 8-bit two's complement, what pattern represents −1?

Name it

Number formats

Binary. Bit k is worth 2k: 10112=8+2+1=11. An n-bit unsigned number spans 0 to 2n−1.

Hexadecimal. Base 16, digits 0 to 9 then A to F (10 to 15). Each hex digit is exactly four bits, so converting is just grouping:

110101102=D616=13×16+6=214

It's written 0xD6 in C and Verilog's 8'hD6.

Two's complement. To represent −m in n bits, store 2n−m. Equivalently, invert every bit of m and add 1. The top bit then acts as a sign bit, worth −2n−1:

  • range: −2n−1 to 2n−1−1 (−128 to 127 for a byte);
  • the pattern for −m plus the pattern for m gives 2n, which overflows to 0: addition just works;
  • subtraction is adding the two's complement, so one adder circuit does both.

Overflow. If two numbers of the same sign add to a result of the other sign, the true answer didn't fit: 100 + 100 in signed 8 bits gives −56.

Sign extension. To widen a signed number, copy its top bit into the new bits: −3 is 1101 in 4 bits and 1111 1101 in 8.

Worked example

What does the 8-bit pattern 0xB6 mean, unsigned and in two's complement?

Show the solution
  1. Unsigned value. 0xB6=101101102=128+32+16+4+2=182
  2. Two's-complement value. Its top bit is 1, so subtract 256: 182−256=−74

Answer 182 unsigned, and −74 in two's complement.

Worked example

Write −45 as an 8-bit two's-complement number, in binary and hex.

Show the solution
  1. Write 45 in binary. 45=001011012
  2. Invert every bit. 11010010
  3. Add 1. 11010011=0xD3
  4. Check. 0xD3 is 211 unsigned, and 211−256=−45

Answer −45 is 1101 0011 in binary, 0xD3 in hex.

Worked example

Add 0x64 and 0x50 as 8-bit two's-complement numbers. What's the result, and has it overflowed?

Show the solution
  1. Convert to decimal. 0x64 = 100 and 0x50 = 80.
  2. Add. 100+80=180=0xB4 which fits in 8 bits with no carry out.
  3. Read it as signed. Its top bit is 1, so as a signed number it reads 180−256=−76
  4. Spot the overflow. Two positive numbers gave a negative result: signed overflow, since +180 is beyond the 8-bit range of −128 to +127.
  5. The rule. Overflow happens exactly when two operands of the same sign give a result of the other sign; the carry out of the top bit tells you only about unsigned overflow.

Answer the result is 0xB4, which reads as −76 signed: the addition has overflowed.

Check

Write 92 as an 8-bit binary number.

Check

What is the binary number 01011100 in decimal?

Check

Write 102 in hexadecimal.

Check

Write −23 in 8-bit two's complement.

Check

The 8-bit pattern 10101101 is read as a two's-complement signed number. What value is it?

Check

Two positive 8-bit two's-complement numbers, 110 and 30, are added.

(a)

What signed value does the 8-bit result read as?

(b)

What does the same 8-bit pattern read as unsigned?

Check

Numbers are stored in n bits.

Step 1

What's the largest unsigned value?

Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.

Explain why

Why does two's complement let the same adder circuit handle both positive and negative numbers?

Practice

Keep practicing

9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.