I wrote this article in 2021 on Hashnode as I was wrapping my head around different spatial referencing systems. I am reposting it here to archive it. Hopefully it helps with understanding the basics that underly spatial grids like [[Bing Tiles]]
Here, we will dive into binary and its default representation: hexadecimal. Binary is the core numeral system used in computers. To do anything, your data or lines of code must be transformed into binary. It is binary that interfaces directly with your computer’s hardware and allows your computer to do things.
Do you need to understand binary? Hopefully not! The history of computing, in a sense, has been a progression away from needing to understand the inner workings of a computer through layers of increasing abstraction (compilers, programming languages, operating systems, graphical user interfaces, etc.). Nonetheless, understanding binary will help you develop your understanding of the way that computers store data and perform calculations.
1. Understanding Binary
The most basic description of binary, of course, is that it is a base-2 numeral system, meaning that the entire system is represented using two numerals: 0 and 1. We are more familiar with the decimal numeral system (a base-10 system) which has 10 numerals: 0–9. Binary works exactly the same as the decimal system, but, because there are only 2 numerals, we quickly need more digits to represent larger numbers, compared to counting numbers in decimal. From here on, we will refer to a single digit of binary, which can have the value 0 or 1, as a “bit.”
When we count from 0 to 10 in the decimal system, we use the numbers 0 through 9, then add a new digit in the “tens” position and start counting our numerals over again in the “ones” position:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
In this case (obviously), we have added a new digit and started counting again from the beginning of our numeral system.
In binary, we perform the same steps, but we quickly run out of new numerals to use:
0, 1, 10
Therefore, we add a new digit and continue counting (the above represents 0, 1, 2 in decimal).
This continues for higher numbers, and we need to add new digits:
0, 1, 10, 11, 100, 101, 110, etc.
In fact, we can represent the number of bits that is needed to store a decimal number in binary as:
⌈log2(10^n)⌉
Where n is the number of digits in the decimal number and⌈⌉denotes a ceiling function. Using this equation, we find that we need 14 bits to represent the number 1000 in binary.
2. Using Binary
A single bit can only store a small amount of information. To do anything actually useful with bits, we need to use a collection of them. For convenience, we refer to a collection of 8 bits as a “byte.”
1 byte: 0000 0000
Why does a byte have 8 bits? It has to do with the original encoding of characters in a computer. Importantly, a byte has the ability to represent the decimal numbers 0 to 255. Because there are 2⁸ combinations of bits in a byte, we can represent 256 numbers using a single byte:
0000 0000, 0000 0001, … , 1111 1111
This is a useful amount of storage, and is the basis for the other standard quantities of computer memory (Kilobytes, Megabytes, Gigabytes, Terabytes, Petabytes).
3. Representing Binary
Nobody except your computer wants to look at binary. Because of the large number of bits required to store information in binary, it becomes very hard for humans to work with. Imagine writing a computer program in binary! It is also onerous to print out lots of binary from a computer program. To address this, we use a simple system to represent binary: hexadecimal.
Using four bits, we have the ability to represent 16 numbers (2⁴):
0000, 0001, … , 1111
If we extend the decimal numeral system a bit to include more characters, we could represent each of these combinations of bits with a single numeral:
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F
Hence, the hexadecimal system (a base-16 numeral system: “hex” — a — “dec” — imal) with the numerals from 0–9 and the characters A-F. Instead of using 0000
, we could simply use 0
, and instead of 1111
, we could use F
.
Find the whole table here (4 bits are called a “nibble,” for your information).
Using hexadecimal, we can represent:
0000 0100
Simply as:
04
That saves space and is easier for a human to interpret. Using hexadecimal, the representation of a byte goes from 8 to 2 digits. In Python for example, a byte is represented as two digits of hexadecimal (with a \x prefix):
\x8a
4. Conclusion
Understanding binary is like taking a deep dive into the way your computer sees the world. Luckily, there is little need for most people to interface directly with binary. It is still helpful to understand exactly how information is represented in a computer. This representation underlies the core data structures of computer science, and operations on bits are what make all computation possible.
Sources
en.wikipedia.org/wiki/Binary_number
stackoverflow.com/questions/7150035/calcula..