seegongsik
Structure

How does a computer store negative numbers

So far we've only stored positive whole numbers like 0, 1, and 2. So how do you store things like minus 5 degrees, a negative balance, or 3.14 in 0s and 1s? There are two clever tricks.

01

Use the first switch as the sign

The first idea for negative numbers is simple.
Set one switch aside just for the sign.
If the first position is 0 the number is positive, if it's 1 it's negative, and the rest of the switches show the size.

sign
0
64
0
32
0
16
0
8
0
4
1
2
0
1
1
Reads as+5

The first cell is the sign; the rest show the size.

Change only the first cell and +5 becomes -5.
It's simple, but it's awkward.
You have to track the sign separately in every calculation, and you even end up with two zeros.
+0 and -0.

02

Roll the numbers around to get negatives

So the method computers actually use is a bit cleverer.
Think of a car's odometer.
Go 1 past 9999 and it rolls over to 0000.
Go back 1 from 0 and it rolls around to the very last number.
Using that, the numbers at the far end were set aside to mean negatives.

0
0000 0000
-1280+127

The same bits, once they roll past the top, are read as a negative number.

This method is called two's complement.
The nice part is that to add a negative number, the adder circuit just adds normally and still gets the right answer.
No separate subtraction circuit is needed, which is why computers chose this method.

03

The decimal point floats

Next up are decimals like 3.14.
The secret is in the name: the point isn't fixed in one place; it floats.
With the same digits, where you put the point decides whether you get a big number or a small one.
Below, you can move the point around inside the digits 314.

3.14
Now it reads 3.14

Move the point and the same digits take on a different value.

Move the point left and the number gets smaller; move it right and it gets bigger.
Because the point floats like this, everything from huge numbers to tiny ones can be stored the same way.
That's why it's called floating point.

04

Split it into sign, point position, and digits

So how do you store a floating point in 0s and 1s?
The switches are split into three parts.
First comes the sign you just saw, then the position of the point (how big the number is), and the rest holds the actual digits.

Sign
Point position
Actual number
Sign · Positive or negative (1 slot)
Point position · Where the point floats (a few slots)
Actual number · The core (the rest)

The switches are divided into three zones.

With just these three pieces, you can write any number in 0s and 1s, wherever its point sits.
It's the same idea as the scientific notation used in science class.
It's like splitting 3.14 into 0.314 times 10.

05

When a computer can't hold 0.1 exactly

Let's finish with a fun fact.
Ask a computer to add 0.1 and 0.2 and sometimes you don't get exactly 0.3.
It's off by a tiny bit, because 0.1 can't be stored exactly in 0s and 1s.

0.1 + 0.2 = 0.30000000000000004

To us it's 0.3, but inside the computer a tiny error remains.

Write 1 divided by 3 in decimal and you get 0.3333..., going on forever.
In the world of 0s and 1s, 0.1 is a number that goes on forever too.
So it has to be cut off somewhere, and that's where the tiny error comes from.
The computer isn't wrong; it's a limit of storing decimals with 0s and 1s.

In one lineNegative numbers are stored with a sign switch and a roll-around trick, and decimals with a floating point. Minus signs and 3.14 alike are, in the end, 0s and 1s with one more agreement on top.
← Structure
Was this helpful? Support seegongsik