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?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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
回答2:
They probably used hex values because the numbers are easier to remember in hex. For example, 0x7fffffff is the same as 2147483647, but is a lot easier to remember.
回答3:
Hex is a human readable form of the binary the CPU actually uses. When looking at low level commands it often makes more sense to match the CPU and think in hex,