Representing (or encoding) a number means to express it in a binary form. Representing numbers in a computer is necessary in order for it to be able to store and manipulate them. However, the problem is that a mathematical number can be infinite, but the representation of a number in a computer must occupy a predefined number of bits. The key, then, is being able to predefine a number of bits, and how to interpret them, so that they can represent the figure as efficiently as possible. In this article, we discuss the representation of natural and real numbers and integers in a computer.
A natural number is a positive integer or zero. The choice of how many bits to use depends on the range of numbers to be used. To encode the natural numbers between 0 and 255, all that is needed is 8 bits (a byte) as 2^{8}=256. Generally speaking, n-bit encoding can be used for representing natural numbers between 0 and 2^{n}-1.
To represent a natural number, having defined how many bits will be used to code it, arrange the bits into a binary cell, with each bit placed according to its binary weight from right to left, then fill the unused bits with zeroes.
An integer is a whole number that can be negative. The number must therefore be encoded in such a way as to tell if it is positive or negative, and to follow the rules of addition. The trick involves using an encoding method called two's complement.
Generally, the largest positive integer encoded using n bits will be 2^{n-1}-1.
The principle of two's complement:
Note that by adding a number and its two’s complement the result is 0.
Let's see this demonstrated in an example:
We want to encode the value -5 using 8 bits. To do so:
Comments:
The highest-weighted bit is 1, so it is indeed a negative number.
If you add 5 and -5 (00000101 and 11111011) the sum is 0 (with remainder 1).
The goal is to represent a number with a decimal point in binary (for example, 101.01, that is not read one hundred one point zero one because it is in fact a binary number, i.e. 5.25 in decimal) using the form 1.XXXXX... * 2^{n} (in our example, 1.0101*2^{2}). IEEE standard 754 defines how to encode a real number.
This standard offers a way to code a number using 32 bits, and defines three components:
Thus, the coding follows the form:
seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
However, there are some restrictions for exponents:
The exponent 11111111 is forbidden.
However, they are sometimes used to report errors. This numeric configuration is called NaN, for Not a number.
Thus, the formula for expressing real numbers is:
(-1)^S * 2^( E - 127 ) * ( 1 + F )
Where:
Here is an example: the value 525.5 is to be encoded.
00000110110000000000000.
0 1000 1000 00000110110000000000000
0100 0100 0000 0011 0110 0000 0000 0000 (4403600 in hexadecimal)
Here is another example, this time using a negative real number: the value -0.625 is to be encoded.
1 1111 1110 01000000000000000000000
1111 1111 0010 0000 0000 0000 0000 0000 (FF 20 00 00 in hexadecimal)
Photo: Unsplash
DON'T MISS