NEGATIVE NUMBERS AND TWO COMPLEMENT

sistema binario

sistema binario

The two’s complement, or base’s complement, is the most common method for representing signed numbers in computer science. The expression two’s complement is often used improperly to denote the negation operation (change of sign) in computers that use this method. Its enormous diffusion is given by the fact that the addition and subtraction circuits do not have to examine the sign of a number represented with this system to determine which of the two operations is necessary, allowing simpler and more precise technologies; only one circuit, the adder, is used for both addition and subtraction. With two’s complement, the most significant bit of the number has a negative or positive weight; from this it follows that all numbers starting with a “1” are negative binary numbers, while all numbers starting with a “0” are positive binary numbers.

A positive binary number can be made negative by inverting its bits and adding 1 to the resulting value. This is mathematically justifiable if we observe how the sum of a binary number and its inverse behaves: the result is a sequence, which in complement to 2 represents -1. In symbols:

In the same way, the absolute value of a negative binary number can be obtained, ie by taking the complementary (inverting the value of the individual bits) and adding 1 to the resulting binary number. An n-digit binary number can represent numbers between -2(n-1) and +2(n-1) -1 using this method. For example, an 8-digit binary number can represent numbers between -128 and +127. This method allows to have a single representation of zero (when all bits are zero, thus eliminating the redundancy of zero that occurs with the representation in modulo and sign), and to operate efficiently addition and subtraction always having the first bit a indicate the sign. In fact, if the most significant bit (the first) is equal to 1, the two’s complement number will be negative, while if this is equal to zero the number will be positive, here is an example:

01101100 (108) 10010100 (-108)

CALCULATION OF THE OPPOSITE IN COMPLEMENT TO TWO

To represent the opposite of a binary number in complement, the single bits are inverted or negated: that is, the logical operation NOT is applied. Finally, 1 is added to the value of the number found with this operation. Let’s take an example by representing the number -8 with 8 bits in two’s complement. Let’s start with the binary representation of the number 8:

0000 1000 (8)

The first digit is 0, so the number is definitely positive. Let’s invert the bits: 0 becomes 1, and 1 becomes 0:

1111 01111

At this point we have obtained the one’s complement of the number 8; to get two’s complement we add 1 to this number:

1111 0111 + 0000 0001 = 1111 0110 (-8)

The result is a signed binary number that represents the negative number -8 in two’s complement. The first bit, equal to 1, highlights that the number is negative.

The two’s complement of a negative number returns its positive number equal to the absolute value: inverting the bits of the representation of the number -8 (above) and adding 1 we obtain:

0000 1001 add 1

0000 1001 +1 = 0000 1000 (+8)

ADDITION

Doing the addition of two integers represented with this method does not require special processes if they are of opposite sign, and the sign is determined automatically. Let’s take an example by adding 10 and -5:

OPERATION: 10+(-5) = 5

1 1 1 1 1 1 CARRY OVER
0 0 0 0 0 1 0 (10)
1 1 1 1 1 0 1 1 (-5 cp2)
1 0 0 0 0 0 1 0 1 RESULT

This process plays on the fixed 8-bit length of the representation: a carry of 1 is ignored which would cause an overflow, and the correct result of operation (5) remains.

SUBTRACTION

Let’s take another example with a subtraction with a negative result: 15 – 35 = −20:

1 1 1 1 1 CARRY OVER
0 0 0 0 1 1 1 1 15
0 1 0 1 1 1 0 1 -35 (cp2)
0 1 1 0 1 1 0 0 RESULT
1 0 0 1 0 1 0 0 -20 cp2( result)

There is also a faster way to calculate the two’s complement, in practice you start from the right and scroll through the bits until you meet the first 1 included and all the others are inverted.

THE FLOATING POINT NUMBERS

Suppose we want to transform the real number 315.226 into the corresponding binary representation. It is very important to choose how many bits will represent the fractional part 0.226. Suppose we want to use 5 bits.

 LET’S SEE THE VARIOUS STEPS

  • First step turns the whole part into binary.

(315)10 = (000100111011)2

  • Second step in the conversion from a natural number into the corresponding binary one proceeded by dividing by two, if there was a remainder it was represented with a 1, otherwise with 0. With real numbers one proceeds by multiplying by two, if the whole number is greater than 1 then take the fractional part.
0.226 x 2 = 0.452 0
0.452 x 2 = 0.904 0
0.904 x 2 = 1.808 1 I take the fractional part of the number
0.808 x 2 = 1.616 1 I take the fractional part of the number
0.616 x 2 = 1.232 1 I take the fractional part of the number

(0.226)10 = (0.00111)2

  • Third step the results come together.

(315.226)10 = (000100111011)2 +(0.00111)2

Let’s see how the conversion from binary to decimal of a real number takes place.

8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5
1 0 0 1 1 1 0 1 1 . 0 0 1 1 1

1×28 + 1×25 + 1×24 + 1×23 + 1×21 + 1×20 + 0x2-1 + 0x2-2 + 1×2-3 + 1×2-4 + 1×2-5 =315.21875

If we want greater precision, we increase the bits by the decimal part.

STANDARD IEEE 754

The IEEE standard for floating-point computation (IEEE 754) (officially: IEEE Standard for Binary Floating-Point Arithmetic (ANSI/IEEE Std 754-1985) or also IEC 60559: 1989, Binary floating-point arithmetic for microprocessor systems) is the most widespread standard in the field of automatic calculation. This standard defines the format for the representation of floating point numbers (including ± 0 and denormalized numbers; infinitives and NaNs, “not a number”), and a set of operations that can be performed on these. It also specifies four rounding methods and describes five exceptions. There are four formats for floating point numbers in this standard: single precision (32 bit), double precision (64 bit), extended single precision (≥ 43 bit), rarely used, and extended double precision (≥ 79 bit), usually supported with 80 bits. Single precision is the minimum required by the standard, the others are optional.

Set the format for numbers that use 32bit, 64bit, 128bit.

THE SCIENTIFIC NOTATION

Let’s consider the number 5000. This number can be expressed in one of the following forms:

5×103   = 5 E +3 = 0.5 E +4 = 50000 E -1

The one just written represents the scientific notation of a number.

0.005 = 5 E -3 = 5 x 10-3

5 is the mantissa

E is the basis

-3 is the exponent

Changing the number changes the mantissa and the exponent, so we have to decide how many bits to give to the mantissa and how many to the exponent.

LET’S SEE A CASE OF APPLYING TO A  NINARY NUMBER

1011.0011 = 10.110011 x 2  moving the point two places to the left is the same as multiplying the number by 4.

101100.11 x 2-2 moving the point two places to the right is the same as dividing the number by 4.

The following form has been established in the IEEE 754 standard:

S 1.XXXXXXX…X2exponent (Normalized scientific notation because it only has 1 in the integer part).

S = sign

X = 0 or 1

We must always arrive at this form. If we think about it, we always find a 1 on the left, because if the binary number starts with 0 we can omit these zeros.

0.00001011 = 1.011×2-5  We see that it is always possible to arrive at normalized scientific notation, now all that remains is to decide how many bits to make available for the mantissa and how many for the exponent.

SINGLE ACCURACY 32 BIT
S EXPONENT MANTISSA
1 bit 8 bit 23 bit
DOUBLE PRECISION 64 BIT
S EXPONENT MANTISSA
1 bit 11 bit 52 bit
EXTENDED PRECISION 80 BIT
S EXPONENT MANTISSA
1 bit 15 bit 64 bit

REPRESENTATION OF THE EXPONENTIAL PART

Now let’s see how to represent the exponential part, this can be positive or negative and it would seem spontaneous to use the two’s complement, but this leads to a small problem that occurs when we have to compare two floating point numbers.

NUMBER EXPONENT
1.0×2-1 11111111
1.0×2+1 00000001

THE BIAS

As you can see ½ is represented by a very high binary digit (the two’s complement of +1 is 11111111) and 2 by a very low binary digit (00000001), which makes comparisons difficult in a computer. The solution to this problem is a bias. Let’s see what it consists of. If the number n1 is less than n2 it is true that:

n1 + x

50 + 100 <70 + 100 where n1 = 50, n2 = 70

-50 +100 <70 +100

The bias in the case of a byte is 127. It is a bit as if the range from -127 to + 127 has been moved forward thanks to the 127 position bias. I leave you a link to Wikipedia to learn more about the topic. https://it.wikipedia.org/wiki/IEEE_754

Let’s see an example.

WE WANT TO WRITE THE NUMBER 315.226 IN SINGLE PRECISION

  • It transforms the real number into a binary number as seen above.

(315.226)10 = (100111011.00111)2

  • The number found is written in normalized scientific notation.

1.0011101100111×2+8 il primo bit verrà sottointeso tanto è sempre a 1

  • The bias is added to the exponent.

8+127 = 135

  • I convert the exponent to binary

(135)10 = (10000111)2

S EXPONENT MANTISSA
1 bit 8 bit 23 bit
0 10000111 0011101100111000000000

So the binary number obtained by sequencing sign, exponent, mantissa will be:

0100001110011101100111000000000

LET’S TAKE ANOTHER EXAMPLE

To obtain the floating point representation in single precision of the decimal number ‑109.78125 it is necessary to convert the integer part and the decimal part into binary, obtaining:

1101101.11001
which in normalized notation corresponds to:

1.10110111001×26

The value of the bias 127 must be added to the exponent and therefore the exponent consists of the value 133:

10000101
Since the number is negative, the sign bit must be set to 1.

The values are then obtained:

1 for the sign
10000101 for the exponent
10110111001 completed with zeros for the mantissa (the 1 of the whole part is understood)
and therefore the final representation is

11000010110110111001000000000000

Further information can be found at the link http://informatica.abaluth.com/il-computer/le-informazioni/rappresolazione-dei-numeri-reali-standard-ieee-754/

This instead is a link to see if the results obtained are correct: Converter

NUMBERS IN THE BINARY SYSTEM – LINKS TO PREVIOUS POST

  • IMAGES, VIDEOS AND SOUNDS

Further links:   https://www.youtube.com/watch?v=d0FBpKQK844&list=PL0qAPtx8YtJeGo5g4Esi7tm6kHPRivkvb&index=26