Skip to content
Chethan Yadav_
Computer Architecture

COA/Lesson 1 of 6/7 min read

Number Systems

Binary, octal and hexadecimal conversion, signed numbers with two's complement, overflow, IEEE 754 floating point, BCD and character encodings.


On this page

Number System

A positional number system with base b uses digits 0 to b - 1; each position is worth a power of the base.

(d_n ... d_1 d_0)_b = d_n * b^n + ... + d_1 * b^1 + d_0 * b^0
SystemBaseDigitsUse
Binary20, 1Hardware
Octal80 to 7Unix permissions (chmod 755)
Decimal100 to 9Humans
Hexadecimal160 to 9, A to FMemory addresses, colors, bytes

Binary Number

Computers use binary because transistors have two reliable states. Units:

UnitSize
Bit1 binary digit
Nibble4 bits
Byte8 bits
WordNatural size for the CPU (32 or 64 bits)
KiB / KB1024 bytes (binary) / 1000 bytes (SI)

With n bits you can represent 2^n values: unsigned range 0 to 2^n - 1. A byte holds 0 to 255.

Hexadecimal

Each hex digit is exactly 4 bits, so conversion between binary and hex is grouping:

1101 0111 1010  (binary)
  D    7    A   (hex)  = 0xD7A

Octal groups 3 bits at a time: 110 101 111 = 657 octal.

Base Conversion

Decimal to binary

Repeatedly divide by 2 and read the remainders bottom up.

45 / 2 = 22 r 1
22 / 2 = 11 r 0
11 / 2 =  5 r 1
 5 / 2 =  2 r 1
 2 / 2 =  1 r 0
 1 / 2 =  0 r 1
45 = 101101 (binary)

Binary to decimal

Sum the powers of two: 101101 = 32 + 8 + 4 + 1 = 45.

Fractions

Multiply the fractional part by 2 repeatedly and take the integer parts top down: 0.625 = 0.101 binary. Some decimals like 0.1 have no finite binary representation, which is why 0.1 + 0.2 != 0.3 in floating point.

Signed numbers

Sign-Magnitude

The leftmost bit is the sign, the rest is the magnitude. Simple, but has two zeros (+0 and -0) and addition needs special logic.

One's Complement

Negate by flipping all bits. Still has two zeros; addition needs an end-around carry.

Two's Complement

Negate by flipping all bits and adding 1. Used by virtually all modern CPUs.

 5 = 0000 0101
-5 = 1111 1010 + 1 = 1111 1011

Why it wins:

  • Only one zero.
  • Addition and subtraction use the same circuit as unsigned arithmetic; subtraction is adding the negation.
  • The most significant bit still indicates the sign.

Range with n bits: -2^(n-1) to 2^(n-1) - 1. An 8-bit signed byte is -128 to 127. Note the asymmetry: negating -128 overflows back to -128.

Representation8-bit rangeZeros
Unsigned0 to 2551
Sign-magnitude-127 to 1272
One's complement-127 to 1272
Two's complement-128 to 1271

Sign extension: when widening a signed number, copy the sign bit into the new high bits. 1011 (4-bit -5) becomes 1111 1011 (8-bit -5).

Overflow

The result does not fit in the available bits.

  • Unsigned: a carry out of the most significant bit. 255 + 1 wraps to 0 in 8 bits.
  • Signed (two's complement): adding two positives gives a negative, or two negatives give a positive. Equivalent rule: the carry into the sign bit differs from the carry out of it.

Real bug: the binary search midpoint (lo + hi) / 2 overflows in 32-bit ints for large arrays; use lo + (hi - lo) / 2.

Floating Point

Represents real numbers in scientific notation: value = (-1)^sign * 1.mantissa * 2^(exponent - bias).

IEEE 754

FormatTotal bitsSignExponentMantissa (fraction)BiasDecimal digits
Single (float)321823127About 7
Double64111521023About 15 to 16

Example: -6.5 in single precision.

6.5 = 110.1 (binary) = 1.101 * 2^2
sign = 1
exponent = 2 + 127 = 129 = 1000 0001
mantissa = 101 0000 ... (leading 1 is implicit)
bits = 1 10000001 10100000000000000000000

Special values

Exponent bitsMantissaMeaning
All 00Zero (positive or negative)
All 0Non-zeroDenormal (subnormal) number
All 10Infinity
All 1Non-zeroNaN (not a number)

Pitfalls:

  • Never compare floats with ==; check abs(a - b) against a small epsilon.
  • NaN != NaN is true.
  • Never store money in floats; use integers (cents) or decimal types.
  • Adding a tiny number to a huge one can do nothing (loss of precision).

BCD

Binary Coded Decimal stores each decimal digit in 4 bits: 59 = 0101 1001. Wastes space (6 of 16 patterns unused) but makes decimal display and exact decimal arithmetic easy. Used in clocks, calculators and some financial hardware.

Gray Code

Consecutive values differ in exactly one bit: 000, 001, 011, 010, 110, 111, 101, 100. Prevents glitches in rotary encoders and is used in Karnaugh map ordering. Convert: gray = n XOR (n >> 1).

Endianness

Order of bytes for multi-byte values in memory. Storing 0x12345678 at address 100:

AddressBig-endianLittle-endian
1001278
1013456
1025634
1037812

x86 and most ARM systems are little-endian. Network protocols use big-endian ("network byte order"), so code calls htonl and ntohl when sending integers.

Character encodings

EncodingSizeNotes
ASCII7 bits128 characters; 'A' = 65, 'a' = 97, '0' = 48
UnicodeCode pointsOver a million possible characters
UTF-81 to 4 bytesASCII compatible, dominant on the web
UTF-162 or 4 bytesUsed by Java and JavaScript strings internally

Quick revision

  • Hex digit = 4 bits, octal digit = 3 bits; convert by grouping.
  • Decimal to binary: divide by 2, read remainders bottom up.
  • Two's complement: invert and add 1; range -2^(n-1) to 2^(n-1) - 1; one zero.
  • Signed overflow: two same-sign operands give an opposite-sign result.
  • IEEE 754 single: 1 sign, 8 exponent (bias 127), 23 mantissa; double: 1, 11 (bias 1023), 52.
  • 0.1 is not exact in binary; never compare floats with == or store money in them.
  • Little-endian stores the least significant byte first; network order is big-endian.