Hello, my name is Duck.
At a training session, I heard this explanation during a lecture by a senior student.
He said, "When we express numbers on the computer, we can only take 0 and 1, so we express them in binary numbers. However, if there is a minus sign, we use the two's complement.
This is a simple explanation.
However, I was a little shocked when I thought I understood it just by saying "bits = binary numbers.
So, I thought about how to express numbers in bits.
Representing Positive Numbers
First, let's look at the most basic case of representing only positive numbers, that is, "unsigned" numbers.
For positive numbers, we simply convert them to binary numbers. For example, let's say we have a number that is represented by 8 bits. Since it is 8 to the power of 2, it can represent 256 different numbers.
|
Decimal |
Binary |
Decimal |
Binary |
|
0 |
0000 0000 |
100 |
0110 0100 |
|
1 |
0000 0001 |
... |
... |
|
2 |
0000 0010 |
127 |
0111 1111 |
|
3 |
0000 0011 |
128 |
1000 0000 |
|
... |
... |
129 |
1000 0001 |
|
10 |
0000 1010 |
... |
... |
|
... |
... |
255 |
1111 1111 |
How to represent negative numbers (2's complement)
So how do we represent negative numbers? The main way to represent numbers that contain negative numbers, or "signed" numbers, is to use two's complement.
Two's complement is the representation of a positive number with all bits inverted, plus one. As an example, here is how -7 in 8 bits can be represented.
The table is as follows.
A signed 8-bit number can represent 256 numbers from -128 to 127. The number of possible values is the same, but the range of possible values is different.
|
Decimal |
Binary |
Decimal |
Binary |
|
-128 |
1000 0000 |
0 |
0000 0000 |
|
-127 |
1000 0001 |
1 |
0000 0001 |
|
... |
... |
2 |
0000 0010 |
|
-100 |
1001 1100 |
3 |
0000 0011 |
|
... |
... |
... |
... |
|
-3 |
1111 1101 |
100 |
0110 0100 |
|
-2 |
1111 1110 |
... |
... |
|
-1 |
1111 1111 |
127 |
0111 1111 |
Basically, negative numbers have the most significant bit set to 1. Anything starting with 0 represents a positive number. When using two's complement to represent signed numbers, positive numbers are represented in the same way as unsigned numbers.
The greatest advantage of two's complement is its ease of operation. Consider -1+1, also in 8 bits.
1111 1111 + 0000 0001 = 0000 0000
The result of the operation is 0000 0000 0000 because the ninth bit is not usable. Negative numbers can also be calculated in the same way.
Representation of Negative Numbers (Offset Binary)
There are other ways to represent signed numbers. Although less frequently used than two's complement, there is the offset binary system. This is a method in which a predetermined value (bias value) is subtracted. Using this method, an 8-bit bias value of 128 is shown in the table below.
|
Decimal |
Binary |
Decimal |
Binary |
| -128 | 0000 0000 | 0 | 1000 0000 |
| -127 | 0000 0001 | 1 | 1000 0001 |
| ....... | ........ | 2 | 1000 0010 |
| -2 | 0111 1111 | ... | ... |
| -1 | 1111 1111 | 127 | 1111 1111 |
Simple arithmetic operations cannot be performed as is, but the numbers are expressed in a way that is easy to understand.
And then to the decimal point.
There is more to the training.
Just when I thought I understood two's complement and now had a good grasp of how to represent bits, my senior instructor continued, "Negative numbers are easy like this, but they are much more difficult to represent with a floating point.
But there is a more difficult one called floating point. If you are going to use FPGAs, you need to know about it."
I was made aware of the depth of the bit world. It seems to be quite difficult to master it.
New Engineer's Blush Blog Articles