![idioph-144x144-icon](../assets/favicon/android-icon-144x144.png)
Why do we use hexadecimal? Why we are using HEXADECIMAL values for computer addressing?
![Why do we use hexadecimal? Why we are using HEXADECIMAL values for computer addressing?](../images/information-concept-information-technology-pixelated-yellow-text-digital-wall-background-binary-code-d-render-57392021-66151598977048.jpg)
Hexadecimal refers to the base-16 number system, which consists of 16 unique symbols, in contrast to the ten unique symbols of the commonly used decimal (i.e., base 10) numbering system.The numbers 0 through 9 are the same in both systems; however, the decimal numbers 10 through 15 are represented by the letters A through F. Thus, for example, the decimal number 11 is represented by B in the hexadecimal system and decimal 14 is represented by E.
Just as the location of the digits in a Decimal number represent the amount of ones, tens, hundreds, thousands, etc. in that number; which are the powers of 10. In a similar way, the digits in a Hex number stand for the ones, sixteens (16 to the power of 1), how many 256s (16 to the power of 2), the amount of 4096s (16 to the power of 3), etc. in its numbers.
The hexadecimal system is commonly used by programmers to describe locations in memory because it can represent every byte (i.e., eight bits) as two consecutive hexadecimal digits instead of the eight digits that would be required by binary (i.e., base 2) numbers and the three digits that would be required with decimal numbers. In addition, it is much easier for humans to read hexadecimal numbers than binary numbers, and it is not much more difficult for computer professionals to read hexadecimal numbers than decimal numbers.
Moreover, conversion between hexadecimal and binary numbers is also easy after a little practice. For example, to convert a byte value from hexadecimal to binary, all that is necessary is to translate each individual hexadecimal digit into its four-bit binary equivalent. Hexadecimal numbers are indicated by the addition of either an 0x prefix or an h suffix. For example, the hexadecimal number 0x2F5B translates to the binary number 0010 1111 0101 1011.
int 0x80 is the assembly language instruction that is used to invoke system calls in Linux on x86 (i.e., Intel-compatible) processors. The 0x in it indicates that it is not a decimal 80 but rather a hexadecimal 80 (which is a decimal 128). A system call is a request in a Unix-like operating system made by a process for a service performed by the kernel.
There are a couple obvious reasons why hexadecimal is preferable to the standard binary that computers store at the low level.
- Readability. Hexadecimal uses digits that more closely resemble our usual base-10 counting system and it’s therefore easier to decide at a glance how big a number like e7 is as opposed to 11100111.
- Higher information density. With 2 hexadecimal digits, we can express any number from 0 to 255. To do the same in binary, we need 8 digits. As we get bigger and bigger numbers, we start needing more and more digits and it becomes harder to deal with.
A common use of hexadecimal numbers is to describe colors on web pages. Each of the three primary colors (i.e., red, green and blue) is represented by two hexadecimal digits to create 255 possible values, thus resulting in more than 16 million possible colors. For example, the HTML (hypertext markup language) code telling a browser to render the background color of a web page as red is <body bgcolor="#FF0000">
and that telling it to render the page as white is <body bgcolor="#FFFFFF">
.
The 6 digit hex colour code should be considered in three parts.
- First two digits represents the amount of red in the colour (max FF, or 255)
- The next two digits represent the amount of green in the colour (max FF, or 255)
- The final two digits represent the amount of blue in the colour (max FF, or 255)
By changing the intensities of red, green and blue, we can create almost any colour e.g. orange can be represented as #FFA500, which is (255 red, 165 green, 0 blue).
The Byte
Bytes are units of information that consist of 8 bits. Almost all computers are byte-addressed, meaning all memory is referenced by byte, instead of by bit. This fact means that bytes come up all the time in programming. Using a counting system that can easily convert into bytes is another important requirement for our binary representation. Base 256 is ideal for this, since it’s 2â¸, meaning one digit is exactly one byte. Unfortunately, for reasons we went over above, 256 would have other problems for readability and be very unwieldy to use.
And now we’re getting to why we use hexadecimal. Hexadecimal, base 16, or base 2â´, represents exactly half a byte. Each byte can be fully represented by two hexadecimal digits, no more, no less.
Hexadecimal also fits all of our other specifications:
- It successfully compresses data. one hex digit can represent 0–15, much better than the 0–1 that binary offers.
- It is easy to read. Everyone knows that C comes before E, and that 4 comes before 9.
- It easily converts to bytes. Two hex digits = 1 byte.