How Computers Store Data
Everything is bitsA computer stores everything as bits — 0s and 1s: numbers, letters, even negative numbers and decimals. In the lab below you turn numbers into binary, octal and hexadecimal, see the number behind a character, store a negative number in 8 bits, and find out why 0.1 can never be stored exactly.
- 1 byte8 bits = 256 values
- Hex1 digit = 4 bits
- 'A'is stored as 65
Ready
Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode
The ideas behind the lab
Exactly what you watched happen, in plain steps.
Bits and bytes
A bit is the smallest piece of data: 0 or 1 (off or on). Eight bits make a byte, the unit memory is measured in. Each extra bit doubles the number of values: n bits can hold 2ⁿ different values, so one byte holds 256 (0 to 255).
1 bit 0 or 1 1 byte 8 bits → 2⁸ = 256 values (0–255) 1 KB 1024 bytes (a kilobyte) 1 MB 1024 KB (a megabyte) 1 GB 1024 MB (a gigabyte) int (4 bytes) = 32 bits → about 4.3 billion values
| Decimal (base 10) | Binary (base 2) | Octal (base 8) | Hexadecimal (base 16) |
|---|---|---|---|
| 0 | 0000 0000 | 0 | 0 |
| 1 | 0000 0001 | 1 | 1 |
| 2 | 0000 0010 | 2 | 2 |
| 7 | 0000 0111 | 7 | 7 |
| 8 | 0000 1000 | 10 | 8 |
| 9 | 0000 1001 | 11 | 9 |
| 10 | 0000 1010 | 12 | A |
| 15 | 0000 1111 | 17 | F |
| 16 | 0001 0000 | 20 | 10 |
| 100 | 0110 0100 | 144 | 64 |
| 255 | 1111 1111 | 377 | FF |
Converting between bases
- Decimal → binary: divide by 2 repeatedly; the remainders, read from the bottom up, are the bits.
- Binary → decimal: add the place values (1, 2, 4, 8, 16 …) of the bits that are 1.
- Binary → hex: group 4 bits from the right; each group is one hex digit (0–9, A–F).
- Binary → octal: group 3 bits from the right; each group is one octal digit (0–7).
- In C: 0x1F is hex, 017 is octal (leading 0!), 31 is decimal. printf: %x / %X hex, %o octal.
Characters are numbers
- Each character has a code in the ASCII table (0–127).
- '0'–'9' are 48–57, 'A'–'Z' are 65–90, 'a'–'z' are 97–122, space is 32, '\n' is 10.
- Small letters are 32 after capitals: 'a' − 'A' = 32.
- A digit character is not its value: '7' − '0' gives the number 7.
- Letters of other languages and emoji need more than one byte (Unicode, UTF-8).
Negative numbers and decimals
- Signed numbers use two's complement: to negate, flip every bit and add 1.
- The leftmost bit is the sign: 1 means negative. One byte holds −128 to 127.
- The same bits read as unsigned give a different value: −1 is 255.
- Decimals are stored as sign, exponent and mantissa (IEEE 754): float 32 bits, double 64.
- Most decimals (like 0.1) have no exact binary form, so they are rounded.
Writing numbers in other bases in C
Every line, explained.
#include <stdio.h>
int main(void)
{
int h = 0x1F;
0x means hexadecimal: 1F = 1×16 + 15 = 31.
int o = 017;
A leading 0 means OCTAL: 17 (base 8) = 1×8 + 7 = 15 — not seventeen!
char c = 'A' + 1;
'A' is 65, so c is 66 — the code of 'B'.
printf("%d %d\n", h, o);
Prints 31 15 — %d always prints in decimal.
printf("%x %X %o\n", 255, 255, 8);
Prints ff FF 10: hex in small letters, hex in capitals, octal.
printf("%#x %#o\n", 255, 8);
The # flag adds the prefix: 0xff 010.
printf("%c %d\n", c, c);
Prints B 66: the same byte as a character and as a number.
return 0;
}
Spot the bug
Mistakes almost every beginner makes — and how to fix them.
A leading zero makes octal
int pin = 0123; // 83, not 123! int day = 09; // error
int pin = 123; int day = 9;
Numbers that start with 0 are octal (base 8). 0123 is 83, and 09 is an error because 9 is not an octal digit.
A digit character is not its value
char d = '7'; int value = d; // 55
char d = '7'; int value = d - '0'; // 7
The character '7' is stored as 55. Subtract '0' (48) to get the number 7.
Hex without 0x
int mask = FF;
int mask = 0xFF;
Without the 0x prefix, FF is read as a name (an identifier), so the compiler says FF is undeclared.
Expecting exact decimals
double total = 0.1 + 0.2; if (total == 0.3) ... // false
int paise = 10 + 20; if (paise == 30) ... // exact
0.1 and 0.2 are stored approximately. For money, count whole paise (or cents) in an int.
A negative number in an unsigned type
unsigned char level = -1;
printf("%d", level); // 255
signed char level = -1;
printf("%d", level); // -1
−1 is stored as 1111 1111. Read as unsigned, those bits mean 255.
Check your understanding
Answer each question — you will see the explanation straight away.
What is 13 in binary?
13 = 8 + 4 + 1, so the bits for 8, 4 and 1 are on: 1101.
How many different values can one byte hold?
8 bits give 2⁸ = 256 values: 0 to 255 (unsigned) or −128 to 127 (signed).
What is the decimal value of 0x1A?
1A in hex = 1×16 + 10 = 26.
Which number is stored for the character 'a'?
'a' is 97 in ASCII; 'A' is 65. They differ by 32.
How is −1 stored in one signed byte?
1 = 0000 0001 → flip = 1111 1110 → add 1 = 1111 1111.
How Computers Store Data quiz
Test yourself: 10 questions, 70% to pass. Create a free account to take the quiz, save your progress and work towards the certificate.