Everyone knows that computers run on ones and zeros. This is because CPUs are made up of billions of transistors, which are basically just on-off switches.
Any code you write needs to be processed by a computer and therefore has to be converted to binary instructions to work.
It isn’t just the execution of code that uses binary, it is also used for the storage of everything in memory and any files that you write to disk. Everything is stored as ones and zeros.
Before we get into exactly how binary works, we need to look are existing number system.
Current number system
All our numbers are represented with the digits 0 to 9. So, that is 10 characters in total that we use to represent every number from 0 to infinity.
You probably remember, in school, numbers being represented by number columns.
We can only put up to 9 in each column and once we need to go above that we have to move on to the next column.
This is what we call the base 10 number system. Each column is a power of 10.
Everyone knows the base 10 number system, and it is probably the one everyone uses, as we have 10 fingers.
Binary Number System
What if instead of using 10 characters to represent all the numbers, we only use 2.
This would then be a base 2 number system, if those 2 characters are 0 and 1, then that is what we call binary.
If we go back to our 4 columns. If in the base 10 numbers system each column represents a power of 10, in the base 2 number system each column represents a power of 2.
With these 4 columns, the maximum number we can store in binary would be 1s in each column, which works out as 8 + 4 + 2 + 1 = 15. If you include 0 as well that is 16 numbers in total, 0 to 15.
Binary numbers can also be used to represent letters as well. In the basic ASCII character set, the first 7 digits used to represent are used to represent 128 different characters.
A = 0100 0001 B = 0100 0010 C = 0100 0011 D = 0100 0100 E = 0100 0101
The 8th digit is then added to allow for all the other special characters, for a total of 256 characters in total.
Å = 1100 0101 Æ = 1100 0110 Ö = 1101 0110
Other Number Bases
It is not just binary numbers that are used in programming, we often encode data into other number bases.
Emails are encoded using base64. So unlike binary which use 0 and 1, base 64 uses the characters capital A to Z, lower case a to z, numbers 0 to 9 and the symbols +, / and =.
ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz 0123456789 +/=
Base 32 is typically used a lot as well. The benefit of base32 is that it only uses upper case letters A to Z and the numbers 0 to 7. As there aren’t any special characters, it is great for things like filenames or to be used in URLs.
And of course, we can’t forget base 16 which uses numbers 0 to 9 and letters A to F.
0 = 0 1 = 1 2 = 2 3 = 3 4 = 4 5 = 5 6 = 6 7 = 7 8 = 8 9 = 9 A = 10 B = 11 C = 12 D = 13 E = 14 F = 15
Base 16 is also known as hexadecimal and is most commonly used to represent colours in CSS.
Colours in hexadecimal are written as 3 sets of 2 characters.
Each set represents, Red, Green, and Blue respectively.
As we have 2 characters and a base16 number, the first character on the right is 16 to the power 0 which equals 1 and the second character is 16 to the power 1 which equals 16.
So, the highest number we can represent with two characters in hexadecimal is FF which would be 15 x 16 + 15 x 1 = 255.
Therefore, #FF0000 would be red, #00FF00 would be green and #0000FF would be blue.
This gives us a total of
256 x 256 x 256 = 16,777,216 colours that can be represented with hexadecimal.
We know computers need to store everything as 0 and 1. So, how do computer sizes relate to binary numbers?
Each binary number, 0 or 1, is stored as 1 bit in the computer.
There are then 8 bits to a byte 1000 bytes to a kilobyte 1000 kilobytes to a megabyte.
If we look at datatypes of variables we use in programming. An
unsigned byte can store a number from 0 to 255. A
byte is 8 bits, so it is an 8 digit binary number.
0000 0000 1111 1111
If we look at these as number columns, we have the left most 1 representing 2^7.
If we add these up, it is no surprise we get 255 as the total.
2^7 + 2^6 + 2^5 + 2^4 + 2^3 + 2^2 + 2^1 + 2^0 = 255
Now, what about negative numbers?
signed byte can only store between -128 to 127. The reason for this is we need to use one of the bits to store the sign of the number.
We use the left most bit for this purpose. If the left most bit is a 1 the number is negative and if it is a 0 the number is positive.
0000 0000 = 0 1000 0000 = -128
Signed numbers use what is known as the two’s complement representation.
Numbers 0 to 127 are represented in the same way we did above. However, for negative numbers, we count up from -128.
1000 0000 = -128 1000 0001 = -127 1111 1111 = -1
There are still 256 numbers that can be represented with this 8-bit binary number.
- -128 to -1 = 128 numbers,
- 1 to 127 = 127 numbers
- and 0
The more digits a binary number has, the larger the number that can be stored. So, an
int typically takes up 32 bits, which means it is a 32 digit binary number:
0000 0000 0000 0000 0000 0000 0000 0000
If it is a
signed int we can store numbers just up to over 2 billion and if it is an
unsigned int we can store just over 4 billion.