A number system helps humans to count things. Counting things implies giving a number to the things we are counting. The number is a symbol which describes the quantity of something.
The primary component of a number system is how many different symbols it assigns to numbers. These symbols represent a number in sequential order starting from 0.
The decimal system assigns 10 symbols to numbers.
0 1 2 3 4 5 6 7 8 9
A value is formed by a combination of these 10 symbols.
A digit binds a symbol to a position in the number. This combo is essential to understand the weight of each symbol in the number.
For example the number 123
is represented by the symbols 1
2
and 3
starting from left to right but each symbol contributes differently to the number’s value.
3
: 3 * 10^02
: 2 * 10^11
: 1 * 10^2The most important property we can draw from this is that with each digit we add to a number the value of that number multiplies by the number of symbols we have assigned in that number system (10 in the case of the decimal system.)
Another property we can see is that the number of digits affects how many values we can represent.
A single digit can represent 10 values in decimal.
Two digits can represent 100 values because if we add a digit to a 1 digit number we are adding 10 possible new values to the existing 10 possible values thus multiplying the existing possible values by 10: 10 * 10 = 100
.
10 or 100 possible values never includes the value 10 or 100 respectively because the number 0 has to be considered. Thus 0-9 and 0-99 are the two sets that compose 10 and 100 values respectively.
When we talk about a number is very important to consider the number of digits we will use to represent that number. In theory and in mathematics, an infinite number of digits can be used to describe a number but in computers it’s important to remember that that is not possible.
The number of digits we use determines the possible values we can represent.
Thus 5 digits can represent 10 * 10 * 10 * 10 * 10 = 10^5 = 10000
values in the decimal system.
Same rules apply to binary. Here we only have to think about two symbols, 0
and 1
.
Thus in binary we can represent 2 values with a single digit. With 2 digits we can represent 2 * 2 = 4
values, with 3 digits 2 * 2 * 2 = 8
and so on.
To represent the value 2 we have to add a new digit :
2(binary) = 10
For the binary number 10
the last digit 0
contributes 0 * 2^0 = 0
and the first digit 1
contributes 1 * 2^1 = 2
.
The binary number 10001011
represents:
1 * 2^7 + 1 * 2^3 + 1 * 2^1 + 1 * 2^0 = 128 + 8 + 2 + 1 = 139
To convert a number from decimal to binary we can follow the following process we can ask:
How many binary digits do we need to represent that number? For example 139
would require 8 binary digits because 139
is between the 2^7 (128) and 2^8 (256)
range. Then we can start assigning 0 or 1 at a digit position based on if the remaining value fits in the range the position represents. If 1 is assigned then we subtract the value added by that position from the total.
139 = 1*128 + 0*64 + 0*32 + 0*16 + 1*8 + 0*4 + 1*2 + 1*1 = 1000 1011
To convert the number 1000 1011
to decimal just sum up their weights.
2^7 + 2^3 + 2^1 + 2^0 = 139
Mathematics represent negative numbers by assigning a negative sign to a positive integer. This doubles up a new range of numbers which is fully symmetrical to the positive number range.
It’s therefore implicit that by choosing to represent negative numbers, what you are really doing is halving the positive numbers you can represent with the given number of digits you have already assigned and letting negative numbers take half of your possible values.
How can you differentiate a negative number from a positive number?
One idea can be to just leave things the same, just change how you interpret the range of your possible values.
For example for an 8-digit binary number representing decimal 139:
10001011
If we choose to represent negatives we would have to change the range of 0-255
for 8 digits to -128 to 127
. This implies that the meaning of 10001011
would change. Note that 10001011
still represents the 140th (notice we start from zero) element of our range it’s just that by introducing a new range, its meaning changes.
The 140th element of the range -128 to 127
is the number 11
(128 negatives + 1 zero + 11 positives).
In practice, negatives are represented in computers by the Twos complement interpretation.
This means that the leftmost digit indicates negativity (1 = negative, 0 equals zero or positive).
Let’s choose some interesting numbers from our range of -128 to 127 to see how they are represented:
1000 0000
1111 1111
(-128 + 64 + 32 + 16 + 8+4+2+1 = -128 + 127 = -1)0000 0000
0000 0001
0111 1111
(64 + 32 + 16 + 8 +4 + 2 + 1)The usefulness of the Two’s Complement interpretation is that it makes it easy to convert a negative number to a positive and vice versa. Notice that we can achieve this by inverting all the digits and adding binary 1.
Convert -5 to +5 and back to -5 in binary. - 5 = 1111 1011 inv = 0000 0100 + 1 = 0000 0101 = +5 inv = 1111 1010 + 1 = 1111 1011 = -5
Why does this happen?
Notice that by inverting what we are really doing is this:
Change all the digits. This will convert any negative to a positive thus changing its value for sure. For instance by inverting -128
-127 = 1000 0001 inv = 0111 1110 (126) plus1= 0111 1111 (127) 1 = 0000 0001 inv = 1111 1110 (-2) plus1= 1111 1111 (-1) because we have inversed the negative side which now has 0 and inversed all the positive side we go from the most negative number to the most positive number. Note that for -128 -128 = 1000 0000 inv = 0111 1111 (127) plus1= 1000 0000 (-128) we go back to -128 if we follow the same process because our number of digits cannot represent a bigger number than 127.
Let’s remind ourselves what real numbers are here are some examples:
1 2.77 2/3 pi
How do we represent such a number?
Starting by the limitation of representation in computers is that we have to compromise at some point that is we can’t accurately represent Pi for example which is a given but also a number like 1.232323232323232232323255443433443 might give us a hard time.
Another thing to consider is what information do we need to store for a number like 25.33
?
Things get unveiled if we interpret the number 25.33
in the following manner:
2533 * 10^-2 Here we are expressing the number 25.33 by an the integer 2533 multiplied by 10 to the power of -2.
Quick reminder: x^-y = 1/x^y
thus 10^-2 = 1/10^2
.
So suddenly we see that in order to represent decimals we can do it with the help of integers but we need additional digits to represent the amount of shifting that takes place in the position.
A common way to do this is break down the information of such a number in groups so that one digit represents the sign, one group of digits represents the exponent (shift) and one group the integer.
A more technical term for the components of such a number are sign, mantissa and exponent:
sign * mantissa * base^exp For -154.25: -1 * 15425 * 10^-2 = 154.25
The term floating point number refers to the fact that the mantissa is an integer number with a floating point being represented by an exponents that shifts the radix (universal term for decimal) point to accommodate the number.
It’s import to remember computers use electricity to “understand” things and process them. More precisely they use electrical signals.
Electricity can be measured by voltage but due to environmental factors voltage fluctuates a lot. Therefore, the safest and most cost efficient method has proven to keep things simple (for computers) and make a dual split of the interpretation of an electrical signal. High voltage means one thing and low voltage means another.
So this duality of interpretation of an electrical signal is what humans can represent with a binary system and decide to give it the symbols 0 and 1 but what the computer really understands is low voltage and high voltage.
This also brings us to the notion that analog signals which is the very nature of our world must be converted to digital signals in order to be understood by a computer. An analog signal represents a continuous measurement of something, that is the quantity can have infinite (or very very big) precision.
For example a given digital thermometer measures temperature with accuracy of two decimal digits but an analog thermometer will represent temperature by the quantity of mercury that rises to a given value. That is, if we could measure every mercury atom in the thermometer to indicate a value we would be able to read a more accurate value but it’s important to remember that animals themselves are digital in the sense that they have limited capacity of accuracy in their interpretation of the world.
A single electrical signal which has a binary value of 0 or 1. Computers do not generally process data bit by bit instead bit are grouped in groups of 8 called bytes and bytes are grouped in words.
Typically a word represents the chunks by which a computer architecture processes stuff which nowadays that is 32 bits (4 bytes) or 64 bits (8 bytes).