Question

This is something I have been thinking while reading programming books and in computer science class at school where we learned how to convert decimal values into hexadecimal. Can someone please tell me what are the advantages of using hexadecimal values and why we use them in programmnig? Thank you.

Was it helpful?

Solution

In many cases (like e.g. bit masks) you need to use binary, but binary is hard to read because of its length. Since hexadecimal values can be much easier translated to/from binary than decimals, you could look at hex values as kind of shorthand notation for binary values.

OTHER TIPS

It certainly depends on what you're doing. It comes as an extension of base 2, which you probably are familiar with as essential to computing.

Check this out for a good discussion of several applications...

https://softwareengineering.stackexchange.com/questions/170440/why-use-other-number-bases-when-programming/

The hexadecimal digit corresponds 1:1 to a given pattern of 4 bits. With experience, you can map them from memory. E.g. 0x8 = 1000, 0xF = 1111, correspondingly, 0x8F = 10001111. This is a convenient shorthand where the bit patterns do matter, e.g. in bit maps or when working with i/o ports. To visualize the bit pattern for 169d is in comparison more difficult.

A byte consists of 8 binary digits and is the smallest piece of data that computers normally work with. All other variables a computer works with are constructed from bytes. For example; a single character can be stored in a single byte, and a 32bit integer consists of 4 bytes.

As bytes are so fundamental we want a way to write down their value as neatly and efficiently as possible. One option would be to use binary, but then we would need a lot of digits. This takes up a lot of space and can be confusing when many numbers are written in sequence:

200 201 202 == 11001000 11001001 11001010

Using hexadecimal notation, we can write every byte using just two digits:

200 == C8

Also, as 16 is a power of 2, it is easy to convert between hexadecimal and binary representations in your head. This is useful as sometimes we are only interested in a single bit within the byte. As a simple example, if the first digit of a hexadecimal representation is 0 we know that the first four binary digits are 0.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top