为什么使用十六进制?
嘿! 我正在查看这段代码 http://www.gnu.org/ software/m68hc11/examples/primes_8c-source.html
我注意到在某些情况下他们使用十六进制数字,例如第 134 行:
for (j = 1; val && j <= 0x80; j <<= 1, q++)
现在他们为什么要使用 0x80? 我不太擅长十六进制,但我找到了一个在线十六进制转十进制的方法,它给了我 0x80 的 128。
同样在第 134 行之前,在第 114 行,他们有这样的内容:
small_n = (n & 0xffff0000) == 0;
十六进制转十进制给了我 4294901760 该十六进制数。 所以在这一行中,他们进行了位 AND 并将结果与 0 进行比较?
为什么不直接使用号码呢? 任何人都可以解释一下,并请举例说明其他情况。
我还看到过大行代码,其中只是十六进制数字,但从未真正理解为什么:(
Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html
I noticed that in some situations they used hex numbers, like in line 134:
for (j = 1; val && j <= 0x80; j <<= 1, q++)
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.
Also before line 134, on line 114 they have this:
small_n = (n & 0xffff0000) == 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.
Also I have seen large lines of code where it's just hex numbers and never really understood why :(
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在您引用的两种情况下,数字的位模式很重要,而不是实际的数字。
例如,
在第一种情况下,
随着循环的进行,
j
将变为 1,然后是 2、4、8、16、32、64,最后是 128。以二进制表示,即
0000:0001
、0000:0010
、0000:0100
、0000:1000
、0001:0000
、0010:0000
、0100:0000
和1000:0000
。C(直到 C23)或 C++(直到 C++14)中没有二进制常量选项,但在十六进制中更清晰一些:
0x01
、0x02
、0x04
、0x08
、0x10
、0x20< /code>、
0x40
和0x80
。在第二个例子中,
目标是删除该值的低两个字节。
因此,给定值为 1,234,567,890,我们希望最终得到 1,234,567,168。
在十六进制中,它更清晰:以
0x4996:02d2
开头,以0x4996:0000
结束。In both cases you cite, the bit pattern of the number is important, not the actual number.
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.In binary, that is,
0000:0001
,0000:0010
,0000:0100
,0000:1000
,0001:0000
,0010:0000
,0100:0000
and1000:0000
.There's no option for binary constants in C (until C23) or C++ (until C++14), but it's a bit clearer in Hex:
0x01
,0x02
,0x04
,0x08
,0x10
,0x20
,0x40
, and0x80
.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 with0x4996:0000
.十六进制(或八进制)数字和底层位模式之间存在直接映射,而十进制则不然。 十进制“9”在位模式方面表示不同的内容,具体取决于它所在的列以及它周围的数字 - 它与位模式没有直接关系。 在十六进制中,无论哪一列,“9”始终表示“1001”。 9 = '1001',95 = '*1001*0101' 等等。
作为我 8 位时代的痕迹,我发现十六进制是任何二进制的方便速记。 摆弄是一种垂死的技能。 有一次(大约 10 年前),我在大学三年级时看到一篇网络论文,其中班上只有 10%(大约 50 人中的 5 人)可以计算位掩码。
There's a direct mapping between hex (or octal for that matter) digits and the underlying bit patterns, which is not the case with decimal. A decimal '9' represents something different with respect to bit patterns depending on what column it is in and what numbers surround it - it doesn't have a direct relationship to a bit pattern. In hex, a '9' always means '1001', no matter which column. 9 = '1001', 95 = '*1001*0101' and so forth.
As a vestige of my 8-bit days I find hex a convenient shorthand for anything binary. Bit twiddling is a dying skill. Once (about 10 years ago) I saw a third year networking paper at university where only 10% (5 out of 50 or so) of the people in the class could calculate a bit-mask.
它有点面具。 十六进制值可以轻松查看底层二进制表示形式。 n &
0xffff0000
返回 n 的前 16 位。0xffff0000
表示“二进制中的 16 个 1 和 16 个 0”0x80
表示“1000000”,因此您从“00000001”开始,然后继续将该位向左移动“0000010” 、“0000100”等直到“1000000”its a bit mask. Hex values make it easy to see the underlying binary representation. n &
0xffff0000
returns the top 16 bits of n.0xffff0000
means "16 1s and 16 0s in binary"0x80
means "1000000", so you start with "00000001" and continue shifting that bit over to the left "0000010", "0000100", etc until "1000000"0xffff0000很容易理解,它是32位值中的16次“1”和16次“0”,而4294901760是神奇的。
0xffff0000 is easy to understand that it's 16 times "1" and 16 times "0" in a 32 bit value, while 4294901760 is magic.
我发现令人抓狂的是,C 系列语言始终支持八进制和十六进制,但不支持二进制。 我一直希望他们能够添加对二进制的直接支持:
很多年前/工作之前,在开发一个涉及大量位级数学的项目时,我厌倦了并生成了一个头文件,其中包含所有已定义的常量可能的二进制值最多 8 位:
它有时会使某些位级代码更具可读性。
I find it maddening that the C family of languages have always supported octal and hex but not binary. I have long wished that they would add direct support for binary:
Many years/jobs ago, while working on a project that involved an enormous amount of bit-level math, I got fed up and generated a header file that contained defined constants for all possible binary values up to 8 bits:
It has occasionally made certain bit-level code more readable.
十六进制的最大用途可能是在嵌入式编程中。 十六进制数字用于屏蔽硬件寄存器中的各个位,或将多个数值拆分打包到单个 8、16 或 32 位寄存器中。
当指定单独的位掩码时,很多人从以下开始:
一段时间后,他们前进到:
然后他们学会作弊,并让编译器生成值作为编译时优化的一部分:
The single biggest use of hex is probably in embedded programming. Hex numbers are used to mask off individual bits in hardware registers, or split multiple numeric values packed into a single 8, 16, or 32-bit register.
When specifying individual bit masks, a lot of people start out by:
After a while, they advance to:
Then they learn to cheat, and let the compiler generate the values as part of compile time optimization:
有时,十六进制值的可视化表示使代码更易于阅读或理解。 例如,当查看数字的十进制表示形式时,位掩码或位的使用变得不明显。
有时这可能与特定值类型必须提供的空间量有关,因此这也可能发挥作用。
一个典型的例子可能是二进制设置,因此我们不使用十进制来显示某些值,而是使用二进制。
假设一个对象有一组非排他性的属性,这些属性的值为 on 或 off(其中 3 个) - 表示这些属性状态的一种方法是使用 3 位。
有效的表示形式是十进制的 0 到 7,但这并不那么明显。 更明显的是二进制表示:
000, 001, 010, 011, 100, 101, 110, 111
另外,有些人对十六进制非常满意。 另请注意,硬编码的幻数就是这样,无论使用什么编号系统,它都不是那么重要,
我希望有所帮助。
Sometimes the visual representation of values in HEX makes code more readable or understandable. For instance bitmasking or use of bits becomes non-obvious when looking at decimal representations of numbers.
This can sometimes do with the amount of space a particular value type has to offer, so that may also play a role.
A typical example might be in a binary setting, so instead of using decimal to show some values, we use binary.
let's say an object had a non-exclusive set of properties that had values of either on or off (3 of them) - one way to represent the state of those properties is with 3 bits.
valid representations are 0 through 7 in decimal, but that is not so obvious. more obvious is the binary representation:
000, 001, 010, 011, 100, 101, 110, 111
Also, some people are just very comfortable with hex. Note also that hard-coded magic numbers are just that and it is not all that important no matter numbering system to use
I hope that helps.
一般来说,使用十六进制数字而不是十进制是因为计算机使用位(二进制数字),当您使用位时,使用十六进制数字也更容易理解,因为从十六进制到二进制比从十进制到二进制更容易。
但
因为
你能看到吗? 从十六进制传递到二进制要简单得多。
Generally the use of Hex numbers instead of Decimal it's because the computer works with bits (binary numbers) and when you're working with bits also is more understandable to use Hexadecimal numbers, because is easier going from Hex to binary that from Decimal to binary.
but
because
Can you see that? It's much more simple to pass from Hex to binary.
一个字节有 8 位。 十六进制,基数为 16,很简洁。 任何可能的字节值都使用集合 0..9 中的两个字符加上 a、b、c、d、e、f 来表示。
Base 256 会更简洁。 每个可能的字节都可以有自己的单个字符,但大多数人类语言不使用 256 个字符,因此十六进制是赢家。
要了解简洁的重要性,请回想一下 20 世纪 70 年代,当您想要检查兆字节内存时,它是以十六进制打印出来的。 打印输出将使用数千页大纸。 八进制会浪费更多的树木。
There are 8 bits in a byte. Hex, base 16, is terse. Any possible byte value is expressed using two characters from the collection 0..9, plus a,b,c,d,e,f.
Base 256 would be more terse. Every possible byte could have its own single character, but most human languages don't use 256 characters, so Hex is the winner.
To understand the importance of being terse, consider that back in the 1970's, when you wanted to examine your megabyte of memory, it was printed out in hex. The printout would use several thousand pages of big paper. Octal would have wasted even more trees.
Hex 或十六进制数字表示 4 位数据,0 到 15 或十六进制 0 到 F。两个十六进制值表示一个字节。
Hex, or hexadecimal, numbers represent 4 bits of data, 0 to 15 or in HEX 0 to F. Two hex values represent a byte.
更准确地说,十六进制和十进制都是数字。 基数(基数 10、16 等)是以更清晰或更方便的方式呈现这些数字的方式。
当讨论“某物有多少”时,我们通常使用十进制。 当我们查看计算机上的地址或位模式时,通常首选十六进制,因为通常各个字节的含义可能很重要。
十六进制(和八进制)具有它们是 2 的幂的属性,因此它们可以很好地映射位分组。 十六进制将 4 位映射到一个十六进制半字节 (0-F),因此一个字节存储在两个半字节 (00-FF) 中。 八进制在数字设备 (DEC) 和其他旧机器上很流行,但一个八进制数字映射到三位,因此它不能很好地跨越字节边界。
总体而言,基数的选择是一种使编程更容易的方法 - 使用与域最匹配的基数。
To be more precise, hex and decimal, are all NUMBERS. The radix (base 10, 16, etc) are ways to present those numbers in a manner that is either clearer, or more convenient.
When discussing "how many of something there are" we normally use decimal. When we are looking at addresses or bit patterns on computers, hex is usually preferred, because often the meaning of individual bytes might be important.
Hex, (and octal) have the property that they are powers of two, so they map groupings of bit nicely. Hex maps 4 bits to one hex nibble (0-F), so a byte is stored in two nibbles (00-FF). Octal was popular on Digital Equipment (DEC) and other older machines, but one octal digit maps to three bits, so it doesn't cross byte boundaries as nicely.
Overall, the choice of radix is a way to make your programming easier - use the one that matches the domain best.
看看这个文件,这是一些非常糟糕的代码。 希望您擅长 C,而不是将其用作教程...
当您直接在位级别或略高于位级别工作时,十六进制很有用。 例如,在开发驱动程序时,您可以直接查看来自设备的位并调整结果,以便其他人可以读取一致的结果。 它是一种紧凑且相当易于阅读的二进制表示形式。
Looking at the file, that's some pretty groady code. Hope you are good at C and not using it as a tutorial...
Hex is useful when you're directly working at the bit level or just above it. E.g, working on a driver where you're looking directly at the bits coming in from a device and twiddling the results so that someone else can read a coherent result. It's a compact fairly easy-to-read representation of binary.