Skip to main content

Someone wanted to learn this too, so Grasp built them a personal learning path.

You can keep exploring this one or create your own

Number Systems and Two’s-Complement Representation

Hello! This module begins with the number representations that digital circuits store and manipulate. Before designing logic, you need to be able to look at a bit pattern and answer two different questions: What value do these bits represent? and Does that value fit in the available width? Today you’ll convert among decimal, binary, and hexadecimal, then encode and decode fixed-width signed integers using two’s complement.


One value, several ways to write it

Decimal, binary, and hexadecimal are bases. A digit’s position determines its weight: powers of 10 in decimal, powers of 2 in binary, and powers of 16 in hexadecimal. For example, the binary number has value

The subscript identifies the base; it is not part of the stored bits. Hexadecimal uses the digits 0–9 and A–F, with A through F standing for decimal 10 through 15. Thus is . The three expressions , , and describe the same value.

To convert a nonnegative decimal integer into another base, repeatedly divide by that base and record the remainders. Read the remainders in reverse order. For 45 in binary:

Division Remainder
1
0
1
1
0
1

Reading upward gives . For hexadecimal, gives quotient 2 and remainder 13 (D), so the result is .

Numbers and Arithmetic - Computer Science | Cornell Bowers

Read the number-conversion examples in Cornell’s Numbers and Arithmetic. They show repeated division for decimal conversion and the shortcut that connects binary directly to hexadecimal.

In “Number Representations,” find the slides “Convert a base 10 number to a base 2 number” and “Convert a base 10 number to a base 16 number.” Start at “Base conversion via repetitive division” on the binary slide, where the first division uses 637, and follow the worked divisions through the hexadecimal slide’s concluding result, “Thus, 637 = 0x27d.” Then find “Convert a base 2 number to base 8 (oct) or 16 (hex).” Under “Binary to Hexadecimal,” read from the explanation of a four-bit nibble through the example concluding that 637 is 0x27d; follow the grouping rather than the octal material that follows. Notice which end of the number supplies the first remainder and which end you read first in the answer.

One hexadecimal digit corresponds to exactly four binary bits, often called a nibble. That makes hexadecimal a compact way to write bit patterns without changing their value:

The groups and have values 2 and 13 (D). Going the other way, replace each hex digit with four bits: . If the leftmost group has fewer than four bits, add leading zeros for grouping: . Leading zeros do not change an unsigned value, though the stated width will matter once you interpret bits as signed.

In circuit work, binary exposes individual bits; hexadecimal makes long patterns readable; decimal expresses their familiar numerical value. Conversion tells you how these views correspond. Signedness, however, requires one more piece of information: how the fixed-width pattern is interpreted.


Width and signed interpretation

A four-bit pattern has 16 possibilities. Interpreted as unsigned, they represent 0 through 15. Interpreted as signed two’s complement, those same 16 patterns represent through . The bits alone do not announce which interpretation you intend.

Each position shows one four-bit pattern. The outer numbers interpret it as unsigned (0–15); the inner numbers interpret that same pattern as four-bit signed two’s complement (\(-8\)–7). For example, \(1111\) is unsigned 15 or signed \(-1\).

Read clockwise on the diagram from to : both interpretations give 0 through 7. At , they diverge. That pattern is unsigned 8 but signed . At , the interpretations are unsigned 15 and signed . This is why “the leading bit is a sign bit” is only a partial description: in two’s complement, its weight is negative, while the other bit positions retain positive weights.

1.2.12 Worked Examples: Two's Complement Representation

Watch MIT OpenCourseWare’s “Worked Examples: Two’s Complement Representation” for a compact demonstration of signed bit weights, representable range, and negation.

Watch the worked examples. In particular, use negative bit weights to check how a leading 1 changes a value; the range to see why the negative side has one extra value; and negation to connect bit inversion and adding one. When evaluating the leftmost bit, treat its weight as a negative power-of-two weight, such as -2^5, not as a negative exponent.

For an -bit signed two’s-complement number, the leftmost bit has weight ; the remaining positions have weights down to . An eight-bit pattern , for instance, has signed value

As an unsigned eight-bit pattern, its value is . Both calculations are correct under their respective interpretations.


Encoding and decoding signed integers

To encode a nonnegative value, write its ordinary binary representation and pad on the left with zeros to the required width. Thus in eight-bit two’s complement is , or .

For a negative value, first confirm it fits. Then write its positive magnitude at the required width, invert every bit within that width, and add one. For in eight bits:

+45 in 8 bits       0010 1101
Invert all 8 bits   1101 0010
Add 1               1101 0011

So the eight-bit encoding of is , also written . The hexadecimal form is simply a shorter transcription of the bits; D3 is not inherently negative without its width and signed interpretation.

You can decode a negative two’s-complement pattern in either of two ways:

  • Use bit weights. For eight-bit , calculate .
  • Recover the magnitude. Because the leftmost bit is 1, invert all eight bits and add one. This gives , so the original value is .

There is also a useful check: for an -bit pattern with a leading 1, find its unsigned value and subtract . Here, is unsigned 211; . This works because the signed and unsigned interpretations differ only in the weight assigned to the leftmost bit.


Fit the value to the width

For bits, the unsigned range is through . The signed two’s-complement range is

Eight bits therefore hold unsigned values 0–255 or signed values –127. The asymmetry matters: encodes when signed, and positive 128 cannot be encoded as an eight-bit signed value. Similarly, encodes , not a second version of zero.

When increasing a signed value’s width, preserve its value by repeating its leftmost bit: four-bit () becomes eight-bit (); four-bit () becomes (). This is sign extension. Simply dropping bits can change the value: eight-bit is , but its low four bits , interpreted as four-bit signed, mean .

A reliable conversion habit is to state base, width, and signedness whenever the meaning could be ambiguous. For example, “” identifies a hex digit, but “four-bit signed ” identifies the value .


Key takeaways

Position weights let you convert a value to decimal; repeated division converts a nonnegative decimal integer to binary or hexadecimal; and groups of four bits connect binary directly to hex. To encode a negative integer in fixed-width two’s complement, check the signed range, write its positive magnitude at that width, invert the bits, and add one. To decode it, use the negative weight of the leftmost bit or recover the magnitude.

These representations give us the input values for the next lesson, where you’ll build a truth table for a Boolean expression—treating each bit as a logic input rather than as part of a number.

Can't find a good explanation? Sign up and we'll make it for you

Sign up