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?
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).
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With