πŸ“˜ CodingMarble Learn

How Numbers Are Stored in Computer Memory

A computer stores every number as a row of bits. With n bits an unsigned number runs from 0 to 2^n βˆ’ 1. Negative numbers use two's complement, where the top bit has a negative weight (βˆ’128 in 8 bits). If a result does not fit, it wraps round: overflow. Bitwise operations (AND, OR, XOR, NOT) and shifts work on single bits, and a left shift by 1 doubles a number. Decimals use floating point: a sign, an exponent and a mantissa, so many fractions are only close, not exact.

🎬 Step-by-step story

  1. A byte is 8 switches, called bits. Each switch has a weight: 1, 2, 4, 8 up to 128. Add the weights of the ON switches. 00000101 is 4 + 1 = 5.
  2. How do we store βˆ’5? We make the top switch worth βˆ’128. Now 11111011 is βˆ’128 + 64 + 32 + 16 + 8 + 2 + 1 = βˆ’5. This is two's complement.
  3. Start at 127, the biggest signed byte. Add 1. The top switch turns on and the number jumps to βˆ’128. It ran out of room and wrapped round. This is overflow.
  4. Shift every bit one place left. 00000101 becomes 00001010. The number doubles: 5 becomes 10. A right shift halves it. AND, OR, XOR and NOT work on each bit alone.
  5. Decimals need three parts: a sign (red), an exponent (blue) and a mantissa (green). 0 100 1000 means 1.5 Γ— 2 = 3. This is floating point. It is exact only for some fractions.
  6. Your turn. Tap the bits, press +1, shifts and NOT. Watch the unsigned and signed values change together.

Tip: drag the 3D scene to turn it. Use two fingers to zoom.

πŸ€” Common doubts, cleared

Why do we need weights of 1, 2, 4, 8…?

Each place is worth twice the place to its right, just like tens, hundreds, thousands in decimal but with 2 instead of 10. Add the weights of the ON switches to read the number.

Why is the top bit worth βˆ’128 and not just a minus sign?

A separate sign would give two zeros (+0 and βˆ’0) and need special adding rules. With βˆ’128 as a weight, ordinary adding works for all numbers and there is only one zero.

Why is there one more negative number than positive ones?

Half of the 256 patterns have the top bit set: that is βˆ’128 to βˆ’1, 128 numbers. The other half is 0 to 127: that is also 128 patterns, but one of them is zero.

Why does 127 + 1 become βˆ’128?

Adding 1 to 01111111 gives 10000000. The top bit turns on, and its weight is βˆ’128. The number wraps round like an odometer.

Does a left shift always double the number?

Only while no 1 falls off the top. Shifting 10000000 left loses the 1, so the result is not double. Watch the top cube in the 3D.

Why is 0.1 + 0.2 not exactly 0.3?

0.1 and 0.2 have endless binary forms, so the computer keeps only the nearest value it can store. The two small errors add up to a tiny difference.

Bits, bytes and the range of numbers

A bit is one switch: 0 or 1. A byte is 8 bits. Each place has a weight, doubling as you go left: 1, 2, 4, 8, 16, 32, 64, 128.

With n bits there are 2n different patterns. If all of them are used for non-negative numbers (unsigned), the range is 0 to 2n βˆ’ 1. For 8 bits that is 0 to 255. For 16 bits it is 0 to 65 535.

Example: 00101101 = 32 + 8 + 4 + 1 = 45.

Two's complement: storing negative numbers

We need some patterns to mean negative numbers. In two's complement the top bit (the most significant bit) has a negative weight. In 8 bits it is βˆ’128 instead of +128.

So the signed 8-bit range is βˆ’128 to +127. In n bits it is βˆ’2nβˆ’1 to 2nβˆ’1 βˆ’ 1. There is one more negative number than positive ones, and only one zero.

How to write βˆ’x

  1. Write x in binary.
  2. Flip every bit (0 to 1, 1 to 0).
  3. Add 1.

Example: βˆ’5. 5 = 00000101. Flip: 11111010. Add 1: 11111011. Check: βˆ’128 + 64 + 32 + 16 + 8 + 2 + 1 = βˆ’5.

Why computers like it

The same adder circuit adds positive and negative numbers. 5 + (βˆ’5) = 00000101 + 11111011 = 1 00000000. The extra 9th bit is dropped, leaving 0. Subtraction becomes adding the negative.

Overflow: when the answer does not fit

A byte has only 256 patterns. If a result is outside the range, the extra bit is lost and the number wraps round like an odometer. This is overflow.

Signed overflow can be spotted: two positive numbers add to a negative one, or two negative numbers add to a positive one. Real programs avoid it by using more bits (16, 32, 64) or by checking the result first.

Bitwise operations and shifts

Bitwise operations treat a number as a row of bits and work on each column separately.

OperationRuleExample (8 bits)
AND (&)1 only if both bits are 11101 & 1011 = 1001
OR (|)1 if at least one bit is 11101 | 1011 = 1111
XOR (^)1 if the bits are different1101 ^ 1011 = 0110
NOT (~)flip every bit~00000101 = 11111010

Shifts

Uses: AND with a mask picks out some bits, OR sets bits, XOR flips chosen bits, and shifts multiply or divide by powers of 2 very fast.

Floating point: storing decimals

To store numbers like 3.14 or 0.000001 the computer uses floating point, like scientific notation in binary. The bits are split in three parts:

Value = (βˆ’1)sign Γ— 1.mantissa Γ— 2exponent βˆ’ bias.

Our 3D uses a tiny 8-bit version: 1 sign, 3 exponent bits (bias 3), 4 mantissa bits. 0 100 1000: exponent 4 βˆ’ 3 = 1, mantissa 1.5, value 1.5 Γ— 2 = 3. Real computers use 32 bits (float: 1 + 8 + 23) or 64 bits (double: 1 + 11 + 52).

Why 0.1 is not exact

0.1 in binary never ends: 0.000110011… Only a fixed number of bits can be kept, so the stored value is a very close guess. That is why 0.1 + 0.2 shows 0.30000000000000004. Never test floating point numbers with ==; check if they are close enough. Very large exponents give infinity and invalid results give NaN (not a number).

Key formulas and definitions

Worked examples

1. Find the value of 00101101 as an unsigned 8-bit number.

Weights of the ON bits: 32 + 8 + 4 + 1 = 45.

2. Write βˆ’20 as an 8-bit two's complement number.

20 = 00010100. Flip: 11101011. Add 1: 11101100. Check: βˆ’128 + 64 + 32 + 8 + 4 = βˆ’20.

3. What is 11110110 as a signed 8-bit number?

Top bit is 1, so it is negative: βˆ’128 + 64 + 32 + 16 + 4 + 2 = βˆ’10.

4. A signed 8-bit variable holds 120. We add 10. What is stored?

120 + 10 = 130, which is above 127. It wraps: 130 βˆ’ 256 = βˆ’126. In bits: 10000010 = βˆ’128 + 2 = βˆ’126.

5. Find 13 XOR 6 and 5 << 3.

13 = 1101, 6 = 0110. XOR gives 1011 = 11. And 5 << 3 = 5 Γ— 8 = 40 (00101000).

6. In the 8-bit mini-float (1 sign, 3 exponent bits with bias 3, 4 mantissa bits) find the value of 0 011 1100.

Exponent = 3, so 3 βˆ’ 3 = 0. Mantissa 1100 = 12/16 = 0.75, so 1.75. Value = 1.75 Γ— 2^0 = 1.75.

7. What is βˆ’5 >> 1 on a signed 8-bit number?

βˆ’5 = 11111011. Arithmetic right shift copies the top bit: 11111101 = βˆ’3 (the result rounds down, toward βˆ’infinity).

Common mistakes

Practice quiz

1. Range of a signed 8-bit number (two's complement):
2. In two's complement, to get βˆ’x you:
3. A signed byte holds 127. After adding 1 it holds:
4. 5 << 2 equals:
5. Why is 0.1 not stored exactly in floating point?

Practice: answer these yourself

Type or choose your answer, then press Check. Use a hint if you are stuck; the full solution appears after you answer.

Frequently asked questions

What is two's complement in simple words?

It is the way computers store negative whole numbers. The top bit has a negative weight, so βˆ’5 is 11111011 in 8 bits. To get βˆ’x flip the bits of x and add 1.

What is integer overflow?

When a calculation gives a number too big for the bits available, the extra bit is lost and the value wraps round, for example 127 + 1 becomes βˆ’128 in a signed byte.

Why do computers use floating point?

It lets one fixed number of bits store both very big and very small numbers, using a sign, an exponent and a mantissa. The price is that many decimals are only close, not exact.

Learn first

Related lessons

All Computer Science lessons