为什么要使用十六进制? [英] Why use hex?
问题描述
嘿!我一直在寻找这个code。在<一个href=\"http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html\">http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html
Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html
我注意到,在某些情况下,他们在网上用134进制数,如:
I noticed that in some situations they used hex numbers, like in line 134:
for (j = 1; val && j <= 0x80; j <<= 1, q++)
现在为什么他们会使用0x80的?我不六角好,但我发现一个网上十六进制为十进制,它给了我128 0x80的。
Now why would they use the 0x80? I am not that good with hex but I found an online hex to decimal and it gave me 128 for 0x80.
还行前134,上线114他们有这样的:
Also before line 134, on line 114 they have this:
small_n = (n & 0xffff0000) == 0;
十六进制十进制给我4294901760为十六进制数。
所以在这里在这一行,他们正在一点,并将结果进行比较0 ??
The hex to decimal gave me 4294901760 for that hex number. So here in this line they are making a bit AND and comparing the result to 0??
为什么不直接使用多少?
任何人都可以请解释并请给出其他情形的例子。
Why not just use the number? Can anyone please explain and please do give examples of other situations.
此外,我已经看到了大线code的在那里只是十六进制数字,从来没有真正明白为什么:(
Also I have seen large lines of code where it's just hex numbers and never really understood why :(
推荐答案
在你举这两种情况下,数字的位模式是很重要的,而不是实际数目
In both cases you cite, the bit pattern of the number is important, not the actual number.
例如,
在第一种情况下,Ĵ
将是1,则2,4,8,16,32,64和循环的进行最后128。
For example,
In the first case,
j
is going to be 1, then 2, 4, 8, 16, 32, 64 and finally 128 as the loop progresses.
在二进制,即,
0000:0001
, 0000:0010
, 0000:0100
, 0000:1000
, 0001:0000
, 0010:0000
, 0100:0000
和 1000:0000
。
有对C或C ++二进制常量没办法,但它在十六进制更清楚一点: 0×01
, 0×02
, 0×04
, 0x08的
, 0×10
, 0x20的
, 0X40
和 0x80的
。
There's no option for binary constants in C or C++, but it's a bit clearer in Hex:
0x01
, 0x02
, 0x04
, 0x08
, 0x10
, 0x20
, 0x40
, and 0x80
.
在第二个例子中,
目标是删除值的两个低字节。
因此,给予1234567890值我们希望与1234567168落得。结果
在十六进制,它更清晰:以 0x4996开始:02d2
,结束与 0x4996:0000
In the second example,
the goal was to remove the lower two bytes of the value.
So given a value of 1,234,567,890 we want to end up with 1,234,567,168.
In hex, it's clearer: start with 0x4996:02d2
, end with 0x4996:0000
.
这篇关于为什么要使用十六进制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!