Additional code/recording of negative numbers in computer memory

Additional code/recording of negative numbers in computer memory

This short article will consider an additional code – a way of writing signed numbers (both positive and negative). We will not only consider the formal side of the issue, but also see what sense and what intuition is behind this decision, that is, we will try to come to the contemplation of additional code.

To fully understand something, you need to create it. Let’s set ourselves this goal.

In computer memory, we can store only non-negative integers with a finite bit size. Let us have 8 bits, in which we need to fit, according to some rule, both positive and negative numbers.

The first thing that comes to mind is to assign the most significant bit to the sign, let’s say 1 means that the number is negative, and 0 is positive. But in this case, adding, for example, 3 and -1, we get:

000000112 + 100000012 = 100001002 = 13210

which is different from the two expected by us. In addition, this way of writing sign numbers allows the existence of positive and negative zero. Of course, we could process such signed numbers in hardware, but this would greatly complicate the development of processors and increase the computational cost of working with signed numbers. So, we need to come up with such a type of record, in which we got the correct result when we directly add a positive number to a negative one.

It would be logical to divide the entire range of numbers available to us in eight digits in half, so that half is occupied by positive numbers, and the other by their inverses for addition. Let’s take, for example, the number 3. Let’s try to find its inverse by addition, that is, a number that, when added with three, will give us 0. The number 3 in the binary code is written as 00000011. How can we use addition to get zero from it? For this, the result of our addition must be a number 100000000. Since our number is eight digits, the oldest unit will be discarded, and in our eight digits we will have 00000000.

At this point, the reader is invited to stop and think a little, as with 00000011 get the reverse of it for assembly.

In order to 00000011 to get the inverse, you need to first invert all its bits (replace one with zero, and zero with one), because in this case, during addition, we will get 11111111and then add one to get 100000000what we were trying to achieve. So the number -3 in the additional code looks like 11111101. Obviously, we can similarly find the inverse of any of our range of 8-bit numbers, because we are in the ring of modulo 2 subtraction classes8 (If you do not know what the rings of classes of deductions are – score). In order for the record of each number to be unambiguous, positive numbers are considered to be all from 00000001 to 01111111negative – all from 10000000 to 11111111. Otherwise, we wouldn’t know how to interpret the number, e.g. 10000101: either as 133 or as -123. So we can write numbers from -128 to 127 in our 8 bits.

Congratulations, now you know how the computer represents negative numbers, and you will not be surprised that when you invert the bits of a signed number, you perfectly get a negative number with a modulus greater than one.

Inverting the bits of the number 10 in Python

Related posts