notesonly.in

One notebook for every subject — open it anywhere.

Log in

Number Systems and Codes: Complete Beginner's Guide

Computers think in binary because a transistor is happiest fully on or off. Humans think in decimal, and programmers compromised with hexadecimal, a compact base-16 shorthand for binary. Number systems are different ways of writing the same values, plus clever codes for negatives, text and error checking.

Memory addresses and color codes are hex, IP addresses are dotted binary in disguise, and every negative number in your computer is two's complement. Debugging code or reading datasheets without binary/hex fluency is like reading without vowels.

Key Concepts

  • Base (radix): Binary base 2, octal 8, decimal 10, hex 16 (digits 0-9, A-F). Each position is worth base^position.
  • Bits, MSB, LSB: One binary digit; most/least significant bits carry the largest/smallest weights.
  • Conversions: Decimal to binary by repeated division by 2; binary to decimal by positional weights; binary to hex by 4-bit groups.
  • Two's complement: Invert all bits and add 1. n bits span -2^(n-1) to 2^(n-1)-1; 8 bits cover -128 to +127.
  • BCD: Each decimal digit in 4 bits; handy for displays and calculators.
  • Gray code: Adjacent values differ in one bit, so encoders never glitch mid-transition.
  • ASCII: 7-bit text code: 'A' = 65 (0x41), '0' = 48.
  • Parity bit: Extra bit making total ones even (or odd), catching single-bit errors.

Formulas

  • Positional value: Sum of digit x base^position, positions from 0 at the right.
  • Negation: -N = (invert all bits of N) + 1.
  • Ranges: n-bit unsigned: 0 to 2^n - 1; two's complement: -2^(n-1) to 2^(n-1) - 1.
  • Limits: Fixed widths overflow silently: 255 + 1 wraps to 0 in 8 bits; 127 + 1 wraps to -128 signed. Real bugs are born here.

Worked Example

Convert 45 to binary/hex, then find -45 in 8-bit two's complement. Step 1: 45 = 32 + 8 + 4 + 1, so binary 101101. Step 2: pad to 00101101, group: 0010 1101 = 0x2D (check: 2x16 + 13 = 45). Step 3: invert: 11010010. Step 4: add 1: 11010011. Step 5: verify as unsigned: 0xD3 = 211; 211 - 256 = -45. Correct. Hardware performs this invert-and-add billions of times per second.

Common Misconceptions

  • "Binary 10 means ten." It means two. Same symbols, different base; always know your base first.
  • "0x10 is ten." Hex digit A is worth ten, but 0x10 is one sixteen plus zero = sixteen decimal.
  • "Negatives just set the top bit." That is sign-magnitude. Two's complement needs invert-plus-one: setting only the top bit of 00101101 gives -83, not -45.

Real-World Applications

  • Debugging: Addresses and register dumps are hex, four times more compact than binary.
  • Networking: IPv4 is four binary bytes in dotted decimal; MAC addresses are hex.
  • Text/media: ASCII/Unicode encode characters; #FF5733 packs RGB bytes.
  • Hardware: Gray-code encoders, BCD displays, parity-guarded links.

Bases for counting: binary for hardware, hex as its compact shadow, decimal for humans. Signed integers live in two's complement, invert and add one, with 8 bits spanning -128 to +127 and silent wraparound beyond. Codes extend the idea: BCD for displays, Gray for glitch-free encoders, ASCII for text, parity for error detection. Practice until 45 = 101101 = 0x2D and -45 = 11010011 feels automatic; everything digital rests on it.