php 为什么在 65535 上使用 0xffff
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13137151/
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 0xffff over 65535
提问by fishcracker
I've seen some PHP script which uses 0xffffin mt_rand(0, 0xffff). I do echo 0xffff;and results to 65535. Is there any significance of using this 0xffff? And also what is that?
我已经看到它使用了一些PHP脚本为0xFFFF在mt_rand(0, 0xffff)。我这样做echo 0xffff;,结果为65535。使用这个0xffff有什么意义吗?还有那是什么?
回答by Denis Kreshikhin
The hexadecimal notation 0xffffmakes it clear that all bits in the number are 1. The 65535is the same number, but the binary representation isn't as obvious. It's only a count number. So this way of writing numbers has different semantics.
十六进制表示法0xffff清楚地表明数字中的所有位都是1. 该65535是相同的号码,但二进制表示就没有那么明显。这只是一个计数。所以这种写数字的方式有不同的语义。
For example, if you want to declare that the maximum value of some variable must be 65535, then it is best to state this like this:
例如,如果你想声明某个变量的最大值必须是 65535,那么最好这样声明:
$max_value = 65535;
Because that makes it easy for humans to compare it to other values like 65500 (< $max_value) or 66000 (> $max_value).
因为这使人们可以轻松地将其与其他值(如 65500 ( < $max_value) 或 66000 ( > $max_value)进行比较)。
On the contrary for bit fields.
位域则相反。
$default_state = 65535;
This does not tell me that all the bits are one (when in fact they are).
这并没有告诉我所有的位都是一(实际上它们是)。
$default_state = 0xFFFF;
This does: Each hexadecimal digit stands for four bits, and an Fmeans four 1-bits. Therefore 0xFFFFis 0b1111111111111111in binary.
这样做:每个十六进制数字代表四位,而 anF表示四个 1 位。因此0xFFFF是0b1111111111111111二进制的。
Hexadecimal notation is used when the programmer wants to make it clear that the binary representation is of importance, maybe more important than the precise numerical value.
当程序员想要明确二进制表示的重要性时,使用十六进制表示法,可能比精确的数值更重要。
回答by Mark Baker
If your code is doing binary manipulations, using hex values like this is a lot easier to understand in the context compared with decimal; although I wouldn't consider mt_rand as one of those contexts (especially as the lower bound is decimal and it's only the upper bound that's hex in your example)
如果您的代码正在进行二进制操作,与十进制相比,使用这样的十六进制值在上下文中更容易理解;尽管我不会将 mt_rand 视为这些上下文之一(尤其是因为下限是十进制,而在您的示例中它只是上限是十六进制)
回答by Victor Stanciu
It's just the hexadecimal notation for 65535. There is no important difference between the two in the context you provided, but it's easier to remember "0, x and four f characters" than 65535.
它只是 65535 的十六进制表示法。 在您提供的上下文中,两者之间没有重要区别,但是比 65535 更容易记住“0、x 和四个 f 字符”。
回答by Guest0xffff
0xffffis a hexadecimal number. The 0xonly tells you that the value after the 0xis hexadecimal. Hexadecimal has 16 digitsfrom 0 to 9and from a to f.
0xffff是一个十六进制数。该0X只告诉你,以后的值0X是十六进制。十六进制有从0 到 9和从a 到 f 的16 位数字。
To calculate the decimal value of a hexadecimal number you have to multiply the digits value with 16 to the power of the digits place in the number.
要计算十六进制数的十进制值,您必须将数字值乘以 16,再乘以该数字中的位数。
Example:
例子:
ffff = (15*16^3) + (15*16^2) + (15*16^1) + (15*16^0) = 65535
f has a value of 15 and it is a the place 0 1 2 and 3 of the number.
f 的值为 15,它是数字的 0 1 2 和 3 位置。
34a6 = (3*16^3) + (4*16^2) + (10*16^1) + (6*16^0) = 13478
Hope that could help
希望能有所帮助

