C++ 为什么要使用十六进制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/243712/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why use hex?
提问by AntonioCS
Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html
嘿!我在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:
我注意到在某些情况下他们使用了十六进制数字,例如第 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.
现在他们为什么要使用 0x80?我对十六进制不太好,但我发现了一个在线的十进制十六进制,它给了我 128 的 0x80。
Also before line 134, on line 114 they have this:
同样在第 134 行之前,在第 114 行,他们有这个:
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??
十六进制到十进制给了我 4294901760 的十六进制数。所以在这一行中,他们做了一些 AND 并将结果与 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 :(
我也看到过大行代码,其中只是十六进制数字,但从未真正理解为什么:(
回答by James Curran
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.
例如,在第一种情况下,j
随着循环的进行,
将是 1,然后是 2、4、8、16、32、64,最后是 128。
In binary, that is,
在二进制中,即
0000:0001
, 0000:0010
, 0000:0100
, 0000:1000
, 0001:0000
, 0010:0000
, 0100:0000
and 1000:0000
.
0000:0001
,0000:0010
,0000:0100
,0000:1000
,0001:0000
,0010:0000
,0100:0000
和1000:0000
。
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
.
C 或 C++ 中没有二进制常量的选项,但在十六进制中更清晰一些:
0x01
, 0x02
, 0x04
, 0x08
, 0x10
, 0x20
, 0x40
, 和0x80
。
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
.
在第二个示例中,目标是删除值的低两个字节。因此,给定 1,234,567,890 的值,我们希望最终得到 1,234,567,168。
在十六进制中,它更清楚:以 开头0x4996:02d2
,以0x4996:0000
.
回答by Jimmy
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"
它有点面具。十六进制值可以很容易地看到底层的二进制表示。n &0xffff0000
返回n的前 16 位。0xffff0000
表示“二进制中的 16 个 1 和 16 个 0”
0x80
means "1000000", so you start with "00000001" and continue shifting that bit over to the left "0000010", "0000100", etc until "1000000"
0x80
意思是“1000000”,所以你从“00000001”开始,继续向左移动那个位“0000010”、“0000100”等直到“1000000”
回答by ConcernedOfTunbridgeWells
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.
十六进制(或八进制)数字和底层位模式之间存在直接映射,而十进制则不是这种情况。十进制“9”表示与位模式不同的内容,具体取决于它所在的列以及围绕它的数字 - 它与位模式没有直接关系。在十六进制中,无论哪一列,“9”始终表示“1001”。9 = '1001', 95 = '*1001*0101' 等等。
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.
作为我 8 位时代的遗迹,我发现十六进制是任何二进制的方便简写。摆弄位是一项垂死的技能。有一次(大约 10 年前),我在大学看到一篇三年级的网络论文,班上只有 10%(大约 50 名左右)的人可以计算位掩码。
回答by Micha? Piaskowski
0xffff0000 is easy to understand that it's 16 times "1" and 16 times "0" in a 32 bit value, while 4294901760 is magic.
0xffff0000 很容易理解,它是 32 位值中的 16 倍“1”和 16 倍“0”,而 4294901760 是魔术。
回答by Micha? Piaskowski
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:
我发现 C 系列语言一直支持八进制和十六进制但不支持二进制,这让我很生气。我一直希望他们能添加对二进制文件的直接支持:
int mask = 0b00001111;
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:
许多年前/工作之前,在从事一个涉及大量位级数学的项目时,我受够了并生成了一个头文件,其中包含所有可能的二进制值的定义常量,最多 8 位:
#define b0 (0x00)
#define b1 (0x01)
#define b00 (0x00)
#define b01 (0x01)
#define b10 (0x02)
#define b11 (0x03)
#define b000 (0x00)
#define b001 (0x01)
...
#define b11111110 (0xFE)
#define b11111111 (0xFF)
It has occasionally made certain bit-level code more readable.
它偶尔使某些位级代码更具可读性。
回答by mkClark
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.
十六进制的最大用途可能是在嵌入式编程中。十六进制数用于屏蔽硬件寄存器中的单个位,或将多个数值拆分为单个 8、16 或 32 位寄存器。
When specifying individual bit masks, a lot of people start out by:
在指定单个位掩码时,很多人从以下开始:
#define bit_0 1
#define bit_1 2
#define bit_2 4
#define bit_3 8
#define bit_4 16
etc...
After a while, they advance to:
过了一会儿,他们前进到:
#define bit_0 0x01
#define bit_1 0x02
#define bit_2 0x04
#define bit_3 0x08
#define bit_4 0x10
etc...
Then they learn to cheat, and let the compiler generate the values as part of compile time optimization:
然后他们学会作弊,并让编译器生成值作为编译时优化的一部分:
#define bit_0 (1<<0)
#define bit_1 (1<<1)
#define bit_2 (1<<2)
#define bit_3 (1<<3)
#define bit_4 (1<<4)
etc...
回答by Lucas Gabriel Sánchez
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.
通常使用十六进制数而不是十进制数,这是因为计算机处理位(二进制数),当您处理位时,使用十六进制数也更容易理解,因为从十六进制到二进制比从十进制到二进制更容易.
OxFF = 1111 1111 ( F = 1111 )
but
但
255 = 1111 1111
because
因为
255 / 2 = 127 (rest 1)
127 / 2 = 63 (rest 1)
63 / 2 = 31 (rest 1)
... etc
Can you see that? It's much more simple to pass from Hex to binary.
你能看到吗?从十六进制传递到二进制要简单得多。
回答by Tim
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.
假设一个对象具有一组非排他的属性,这些属性的值要么是开要么是关(其中 3 个)——表示这些属性状态的一种方法是用 3 位。
valid representations are 0 through 7 in decimal, but that is not so obvious. more obvious is the binary representation:
有效的表示形式是十进制的 0 到 7,但这并不那么明显。更明显的是二进制表示:
000, 001, 010, 011, 100, 101, 110, 111
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.
我希望这有帮助。
回答by Jim C
Hex, or hexadecimal, numbers represent 4 bits of data, 0 to 15 or in HEX 0 to F. Two hex values represent a byte.
十六进制或十六进制数字表示 4 位数据,0 到 15 或十六进制 0 到 F。两个十六进制值表示一个字节。
回答by dongilmore
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.
一个字节有 8 位。十六进制,基数为 16,很简洁。任何可能的字节值都使用集合 0..9 中的两个字符加上 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.
Base 256 会更简洁。每个可能的字节都可以有自己的单个字符,但大多数人类语言不使用 256 个字符,因此 Hex 是赢家。
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.
要了解简洁的重要性,请回想一下 1970 年代,当您想检查您的内存兆字节时,它以十六进制打印出来。打印输出将使用数千页大纸。Octal 会浪费更多的树。