Learn computing fundamentals, programming, algorithms, systems, and software engineering.
Computers store everything as patterns of two states — on or off — which we write as the digits 1 and 0. That two-state system is binary, and every number, letter, image, and program is ultimately a long string of bits. Because binary is long and hard for people to read, programmers often group bits and write them in hexadecimal instead. This lesson teaches how numbers work in binary and hexadecimal, how to convert between them and decimal, how negative numbers are represented with two’s complement, how text becomes numbers through character encoding, and how bitwise operations manipulate individual bits. These are the building blocks for everything from memory addresses to color values to file formats. You do not need to memorize long conversion tables. The goal is to understand why the system works the way it does, so that a binary number, a hex value, or a bitmask stops looking mysterious.
Digital electronics are reliable when they distinguish two clear states — a voltage that is high or low. Binary maps those two states to 1 and 0. From those two symbols, computers can represent any number, text, image, or instruction.
| System | Base | Digits used | Example |
|---|---|---|---|
| Decimal | 10 | 0–9 | 42 |
| Binary | 2 | 0–1 | 101010 |
| Octal | 8 | 0–7 | 52 |
| Hexadecimal | 16 | 0–9, A–F | 2A |
In any base, each position is a power of that base. The rightmost digit is the base raised to 0, the next is base to the 1, and so on.
Multiply each bit by its place value (a power of two) and add the results.
Hexadecimal groups four bits into one digit, which is why one hex digit (0–F) covers exactly 0000–1111. This makes hex a compact, human-friendly way to read binary.
A bit is one binary digit. A byte is 8 bits. Larger amounts are named with prefixes; be aware that "kilobyte" is sometimes 1000 bytes (SI) and sometimes 1024 bytes (binary).
| Unit | Bits / bytes |
|---|---|
| Bit | 1 binary digit |
| Byte | 8 bits |
| Kilobyte (KB) | 1000 bytes (SI) |
| Kibibyte (KiB) | 1024 bytes (binary) |
| Megabyte (MB) | 1000² bytes |
A fixed number of bits can only represent a fixed range. To represent negative numbers, computers reserve the leftmost bit as the sign. Two’s complement is the scheme that makes addition of positive and negative numbers work with the same hardware as addition of positives.
Text is stored as numbers, so there must be a mapping from characters to numbers. ASCII maps 128 characters (letters, digits, punctuation) to 7-bit values. Unicode is a much larger standard that aims to cover every writing system, and UTF-8 is a widely used way to encode Unicode into bytes.
Bitwise operations act on the individual bits of a value. They are the basis of flags, permissions, and low-level data packing.
| Operation | Symbol | Rule | Example (1 bit) |
|---|---|---|---|
| AND | & | 1 only if both are 1 | 1 & 1 = 1, 1 & 0 = 0 |
| OR | | | 1 if either is 1 | 1 | 0 = 1, 0 | 0 = 0 |
| XOR | ^ | 1 if bits differ | 1 ^ 0 = 1, 1 ^ 1 = 0 |
| NOT | ~ | flip every bit | ~0 = 1 |
Shifting moves bits left or right. A left shift by one doubles the value (in binary); a right shift by one halves it.
The 0b and 0x prefixes tell Python to read the number as binary or hexadecimal.
Work through the divide-by-2 method, then group the bits into hex.
0b101010 0x2a
Use bitwise OR to combine flags and AND to check whether a flag is set.
0b11 True
Confirm the 8-bit two’s complement of -5.
0b11111011