Inside the bits Ready
main.c

                

Ready

Statements run 0 Lines printed 0
Ready

Keyboard: ← → step · Space play / pause · R reset · Esc exit classroom mode

How it works

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
Counting in four bases
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

  1. Decimal → binary: divide by 2 repeatedly; the remainders, read from the bottom up, are the bits.
  2. Binary → decimal: add the place values (1, 2, 4, 8, 16 …) of the bits that are 1.
  3. Binary → hex: group 4 bits from the right; each group is one hex digit (0–9, A–F).
  4. Binary → octal: group 3 bits from the right; each group is one octal digit (0–7).
  5. In C: 0x1F is hex, 017 is octal (leading 0!), 31 is decimal. printf: %x / %X hex, %o octal.

Characters are numbers

  1. Each character has a code in the ASCII table (0–127).
  2. '0'–'9' are 48–57, 'A'–'Z' are 65–90, 'a'–'z' are 97–122, space is 32, '\n' is 10.
  3. Small letters are 32 after capitals: 'a' − 'A' = 32.
  4. A digit character is not its value: '7' − '0' gives the number 7.
  5. Letters of other languages and emoji need more than one byte (Unicode, UTF-8).

Negative numbers and decimals

  1. Signed numbers use two's complement: to negate, flip every bit and add 1.
  2. The leftmost bit is the sign: 1 means negative. One byte holds −128 to 127.
  3. The same bits read as unsigned give a different value: −1 is 255.
  4. Decimals are stored as sign, exponent and mantissa (IEEE 754): float 32 bits, double 64.
  5. Most decimals (like 0.1) have no exact binary form, so they are rounded.
Walkthrough

Writing numbers in other bases in C

Every line, explained.

1 #include <stdio.h>
2
3 int main(void)
4 {
5 int h = 0x1F; 0x means hexadecimal: 1F = 1×16 + 15 = 31.
6 int o = 017; A leading 0 means OCTAL: 17 (base 8) = 1×8 + 7 = 15 — not seventeen!
7 char c = 'A' + 1; 'A' is 65, so c is 66 — the code of 'B'.
8 printf("%d %d\n", h, o); Prints 31 15 — %d always prints in decimal.
9 printf("%x %X %o\n", 255, 255, 8); Prints ff FF 10: hex in small letters, hex in capitals, octal.
10 printf("%#x %#o\n", 255, 8); The # flag adds the prefix: 0xff 010.
11 printf("%c %d\n", c, c); Prints B 66: the same byte as a character and as a number.
12 return 0;
13 }
Common mistakes

Spot the bug

Mistakes almost every beginner makes — and how to fix them.

A leading zero makes octal

✗ Wrong
int pin = 0123;   // 83, not 123!
int day = 09;     // error
✓ Right
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

✗ Wrong
char d = '7';
int value = d;        // 55
✓ Right
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

✗ Wrong
int mask = FF;
✓ Right
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

✗ Wrong
double total = 0.1 + 0.2;
if (total == 0.3) ...   // false
✓ Right
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

✗ Wrong
unsigned char level = -1;
printf("%d", level);   // 255
✓ Right
signed char level = -1;
printf("%d", level);   // -1

−1 is stored as 1111 1111. Read as unsigned, those bits mean 255.

Practice

Check your understanding

Answer each question — you will see the explanation straight away.

0 / 5 correct
Question 1

What is 13 in binary?

Question 2

How many different values can one byte hold?

Question 3

What is the decimal value of 0x1A?

Question 4

Which number is stored for the character 'a'?

Question 5

How is −1 stored in one signed byte?

Assessment

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.