Number TheoryTopic #35 of 40

Number Representations

Binary, octal, hexadecimal systems, base conversion, and computer number representation.

Positional Notation

A number in base bb with digits dndn1d1d0d_n d_{n-1} \ldots d_1 d_0 represents: dnbn+dn1bn1++d1b+d0d_n \cdot b^n + d_{n-1} \cdot b^{n-1} + \cdots + d_1 \cdot b + d_0

Each digit satisfies 0di<b0 \leq d_i < b.

Common Number Systems

BaseNameDigits
2Binary0, 1
8Octal0-7
10Decimal0-9
16Hexadecimal0-9, A-F

Hexadecimal Digits

HexDecBinary
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
A101010
B111011
C121100
D131101
E141110
F151111

Conversion: Base bb to Decimal

Expand using positional notation.

Example: (1011)2(1011)_2 to decimal

123+022+121+120=8+0+2+1=111 \cdot 2^3 + 0 \cdot 2^2 + 1 \cdot 2^1 + 1 \cdot 2^0 = 8 + 0 + 2 + 1 = 11

Example: (2F)16(2F)_{16} to decimal

216+15=32+15=472 \cdot 16 + 15 = 32 + 15 = 47

Conversion: Decimal to Base bb

Repeatedly divide by bb, record remainders (read bottom to top).

Example: 156 to binary

156÷2=78 R 0156 \div 2 = 78 \text{ R } 0 78÷2=39 R 078 \div 2 = 39 \text{ R } 0 39÷2=19 R 139 \div 2 = 19 \text{ R } 1 19÷2=9 R 119 \div 2 = 9 \text{ R } 1 9÷2=4 R 19 \div 2 = 4 \text{ R } 1 4÷2=2 R 04 \div 2 = 2 \text{ R } 0 2÷2=1 R 02 \div 2 = 1 \text{ R } 0 1÷2=0 R 11 \div 2 = 0 \text{ R } 1

Answer: (10011100)2(10011100)_2

Quick Conversions

Binary ↔ Octal

Group binary digits in 3s (from right).

(10011100)2=(010011100)2=(234)8(10011100)_2 = (010|011|100)_2 = (234)_8

Binary ↔ Hexadecimal

Group binary digits in 4s (from right).

(10011100)2=(10011100)2=(9C)16(10011100)_2 = (1001|1100)_2 = (9C)_{16}

Binary Arithmetic

Addition

  1011
+ 0111
------
 10010

Carry: 1 + 1 = 10 (write 0, carry 1)

Subtraction

Use borrowing or two's complement.

Multiplication

Similar to decimal, but simpler (only 0 and 1).

Signed Integer Representations

Sign-Magnitude

First bit is sign (0 = positive, 1 = negative), rest is magnitude.

Problem: Two representations of 0 (+0 and -0).

One's Complement

Negative: flip all bits.

Problem: Still two zeros.

Two's Complement (Most Common)

For nn-bit number:

  • Positive: standard binary
  • Negative: flip bits and add 1 (or 2nx2^n - |x|)

Range: 2n1-2^{n-1} to 2n112^{n-1} - 1

Example (8-bit):

  • 5=000001015 = 00000101
  • 5-5: flip → 11111010, add 1 → 1111101111111011

Advantage: Single zero, addition works for signed numbers.

Converting Negative Numbers

Method 1: Flip bits, add 1 Method 2: x-x in nn bits = 2nx2^n - |x|

Example

6-6 in 8-bit two's complement: 286=2566=250=(11111010)22^8 - 6 = 256 - 6 = 250 = (11111010)_2

Floating-Point Numbers

IEEE 754 Single Precision (32-bit)

SignExponentMantissa
1 bit8 bits23 bits

Value: (1)s×1.m×2e127(-1)^s \times 1.m \times 2^{e-127}

where ss = sign bit, mm = mantissa, ee = biased exponent.

Special Values

  • Zero: e=0e = 0, m=0m = 0
  • Infinity: e=255e = 255, m=0m = 0
  • NaN: e=255e = 255, m0m \neq 0

Precision Limits

  • Single (32-bit): ~7 decimal digits
  • Double (64-bit): ~15 decimal digits

Bit Manipulation

Bitwise Operations

OperationSymbolExample
AND&10101100=10001010 \land 1100 = 1000
OR|10101100=11101010 \lor 1100 = 1110
XOR^10101100=01101010 \oplus 1100 = 0110
NOT~¬1010=0101\neg 1010 = 0101

Shift Operations

Left shift: x<<nx << n multiplies by 2n2^n Right shift: x>>nx >> n divides by 2n2^n (for unsigned)

Common Tricks

Check if bit kk is set: (x >> k) & 1 Set bit kk: x | (1 << k) Clear bit kk: x & ~(1 << k) Toggle bit kk: x ^ (1 << k) Check power of 2: (x & (x-1)) == 0

Powers of 2

2n2^nValue
202^01
2102^{10}1,024 (1 KB)
2202^{20}1,048,576 (1 MB)
2302^{30}1,073,741,824 (1 GB)
2322^{32}4,294,967,296
2642^{64}18,446,744,073,709,551,616