Dive into the world of hexadecimal number system
Probably, many have heard about the hexadecimal number system (aka hexadecimal) and even seen it when working with their code, when instead of the usual zeros and ones, something like 0x0034ab37 appears in the stack trace. What is it, where are those very zeros and ones that the computer works with, and why use the hexadecimal system? Let's figure it out.
A little history
Numbers play an important role in human history, influencing science, culture and everyday life. One of the first number systems that have survived to our times are Roman numerals. This system originated in ancient Rome and was used for centuries to record numbers. For example, the number 6 in the Roman alphabet looks like VI. Roman numerals are convenient for writing on stone and papyrus, and there are not many of them:
![Example of Roman numerals Example of Roman numerals](https://habrastorage.org/getpro/habr/upload_files/7a6/4b5/9c9/7a64b59c98dff3cdd0aa25eb37f994a3.jpg)
Each of these numbers represents a combination of symbols that can be combined to create other numbers. For example, the number 2024 is written as MMXXIV. However, the Roman system has its limitations. It is not suitable for complex mathematical calculations because there is no concept of zero and no convenient way to represent large numbers or perform multiplication and division operations.
Roman numerals were actively used in architecture, numbering chapters of books, on watches and coins. They can still be seen on historical buildings, monuments and even in modern contexts, such as movie titles or product release years.
It is interesting to note that other ancient civilizations, such as the Egyptians and Babylonians, also developed their own unique number systems. For example, Egyptian hieroglyphs included symbols for units, tens, hundreds, and thousands, which allowed them to carry out calculations complex enough to build pyramids and conduct trade.
These ancient number systems are important historical artifacts that help us understand the evolution of human thought and culture.
The transition from Roman numerals to more convenient and universal systems such as Arabic numerals was an important step in the development of mathematics and science, opening the way to modern technology and computing.
Arabic Numerals and the Decimal System
Arabic numerals came to the rescue; they became known to Europeans in the 10th century and are now used in most countries to write numbers in the decimal system.
It is important to note that it is not the number itself that is important here, but the place it is in. I mean that 110 and 101 contain the same set of numbers, but the place they are in affects. This is how we understand that the number 110 is greater than 101, and vice versa.
The decimal number system is the most popular in the world. Perhaps if a person had fewer fingers, then the fivefold or sixfold systems would be more popular, but we have what we have. Having ten fingers on your hand makes it easy to go through all the necessary numbers. But what happens when you run out of fingers? Of course, your toes come to the rescue. And after 10 starts 11, 12 and so on.
![](https://habrastorage.org/getpro/habr/upload_files/5ab/3cd/643/5ab3cd64391f381b0bf89bbcd6533f09.png)
How it works?
Now let's figure out how the number system works. In the decimal system, after the number 9 comes 10, that is, we start again from 1, adding 0 to it. After 19, we change 1 to 2 and start again: 20, 21, 22, and so on. When we get to 99, the order increases, and we get 100. This simple algorithm makes it easy to add and multiply numbers.
The binary system that a computer uses works similarly, but uses only two characters: 0 and 1. After 1 comes 10, then 11, 100, 101, 110, and so on. Addition and multiplication in this system are also simple.
However, imagine that you have two large binary numbers: 11110000110010100111010 (7890234 in decimal) and 10001111111000001101001 (4714601 in decimal). Adding them is not difficult, but writing the result on paper and remembering it is difficult. You can convert these numbers to the decimal system and vice versa, but this is inconvenient. This is where the hexadecimal system comes to the rescue.
What is the hexadecimal system?
The hexadecimal system uses 16 characters: the numbers 0-9 and the letters AF. For example, 10 in decimal becomes A in hexadecimal, 15 becomes F, and 16 becomes 10 because after F there is an increase in order.
![](https://habrastorage.org/getpro/habr/upload_files/57d/c17/6ee/57dc176eef7fd172ac28d7d5c8a29614.png)
Why is the hexadecimal system so popular?
The reason for the popularity of the hexadecimal system is its simplicity and convenience. It allows you to easily write down the number of bits. For example, 4 bits = 2^4 = 16, which corresponds to numbers from 0 to F. One byte can be written as two hexadecimal numbers. For example, the binary number 111010 can be written as 0x3A (0x is the prefix denoting the hexadecimal system).
When we deal with large numbers, such as 11110000110010100111010, it is much more convenient to write them as 0x78653A. This makes the hexadecimal system an indispensable tool in programming and computing tasks.
Conclusion
The hexadecimal number system makes working with large binary numbers more convenient and clear. Its simplicity and efficiency make it an indispensable tool in programming and computing tasks. Now, knowing the basics, you can easily use it in your work.
More interesting content in my telegram “FAANG Engineer's Notes”