Binary code is a system of representing numbers, letters, commands, images and sounds. Amazingly, it uses only two types of information to do this – 1 and 0. The strings of 1’s and 0’s that make up binary code may seem random, but of course they’re not.
Binary code is at the absolute heart of anything that goes on inside a computer – and yet it’s something that most code tutorials don’t cover.
Here’s an explanation of the fundamentals of binary. At the end you should have a basic idea of what all those 1s and 0s mean.
The binary number system is a base-two system, meaning it uses two distinct digits – 0 and 1. The decimal number system we’re all familiar with is a base-ten system, meaning it uses ten distinct digits – 0 and 1, but also 2, 3, 4, 5, 6, 7, 8 and 9.
Counting in the decimal system, when all ten digits have been exhausted, the next number is represented as ’10’. In the binary system, it’s the exact same. After 0 and 1 comes 10. In fact, here are the first 15 numbers expressed in binary:
As you can see, the binary system counts up in a logical order, and whenever a number made up of entirely 1’s is reached, an extra digit is added. This is exactly the same thing that happens in the decimal system when a number made up of entirely 9’s is reached.
As you can also see, there are 16 distinct numbers that can be represented in 4 digits or less (1 through 15 as well as 0). This can be calculated because 16 = 24.
However, binary numbers in a computing context are always represented in 8 digits or less – 8 binary digits forming one byte. The total quantity of distinct numbers that can be represented in 8 digits is 28 = 256. 1 through 255 as well as 0.
So 255 in binary is 11111111.
The hexadecimal system, or hex system, is closely related to the binary system. Here’s why.
To represent the number 255 in binary, it takes 8 digits. That’s a lot, considering the decimal system only needs 3 digits to represent 255. However, the hex system goes one step further and allows the number 255 to be represented in only two digits.
This is because the hex system is base-sixteen, and 162 = 256. In other words, the highest number that can be represented with two digits in hex is 255. In other words still, any 8-digit binary byte can be represented as a 2-digit hex byte.
This is great for both computers and humans. For computers, it saves space, and for humans, it improves readability.
If you’re wondering, the 16 ‘digits’ that the hex system utilizes are the numbers 0 through 9 and then the letters A through F. F is the hex code for 15, and FF is the hex code for 255.
Binary code representing numbers is pretty uniform – there’s only one way to do it. However, there are multiple methods of representing letters and symbols in binary code. These methods are called encodings.
For example, the ASCII encoding assigns unique binary bytes to 128 different characters. This makes it possible to encode any string of text. This is the string ‘Hello World’ encoded in ASCII:
01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100
Each byte represents a letter (except for the one that represents the space between the words, of course). The spaces between the bytes are only there to make the binary more readable for us. Computers do not read them.
Other Applications of Binary
As well as numbers and letters, binary code can represent commands, images and sounds – but that’s beyond the scope of this tutorial. It really is amazing how just two digits can do so much.
Disclosure of Material Connection: Some of the links in the post above are “affiliate links.” This means if you click on the link and purchase the item, I will receive an affiliate commission. Regardless, I only recommend products or services I use personally and believe will add value to my readers.