Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use hex values instead of normal base 10 numbers?

I was looking over this code to calculate math.sqrt in Java. Why did they use hex values in some of the loops and normal values for variables? What benefits are there to use hex?

like image 854
Nope Avatar asked Jan 16 '09 20:01

Nope


People also ask

Why do we use hex instead of binary?

Software developers and system designers widely use hexadecimal numbers because they provide a human-friendly representation of binary-coded values. Each hexadecimal digit represents four bits (binary digits), also known as a nibble (or nybble).

What is the advantage of using hexadecimal numbers?

The main advantage of using Hexadecimal numbers is that it uses less memory to store more numbers, for example it store 256 numbers in two digits whereas decimal number stores 100 numbers in two digits. This number system is also used to represent Computer memory addresses.

What is the purpose of hexadecimal number system?

Hex numbers are compact and use less memory, so more numbers can be stored in computer systems. Their small size also makes input-output handling easier compared to other numbering formats. Because it's easy to convert hexadecimal to binary and vice versa, the system is widely used in computer programming.

Why hexadecimal is used in data representation?

Hexadecimal is commonly used by computers because it can represent a byte of data with just two characters (instead of eight). Each hexadecimal character represents half a byte, also called a nibble.


1 Answers

Because hex corresponds much more closely to bits that decimal numbers. Each hex digit corresponds to 4 bits (a nibble). So, once you've learned the bitmask associated with each hex digit (0-F), you can do something like "I want a mask for the low order byte":

0xff

or, "I want a mask for the bottom 31 bits":

0x7fffffff

Just for reference:

HEX    BIN
0   -> 0000
1   -> 0001
2   -> 0010
3   -> 0011
4   -> 0100
5   -> 0101
6   -> 0110
7   -> 0111
8   -> 1000
9   -> 1001
A   -> 1010
B   -> 1011
C   -> 1100
D   -> 1101
E   -> 1110
F   -> 1111
like image 69
Dave Ray Avatar answered Oct 16 '22 01:10

Dave Ray