0x01 和 0x01f 之间的区别
我正在查看 Identicons 的原始源代码。 有一些代码做了一些调整来提取红色、绿色和蓝色分量:
int blue = (code >> 16) & 0x01f;
int green = (code >> 21) & 0x01f;
int red = (code >> 27) & 0x01f;
代码变量是一个 32 位整数。
我的问题是:数字0x01和0x01f有什么区别?
我假设 f 表示该值是浮点数,但为什么要使其成为浮点数?二进制的浮点表示与整数表示不同吗?如果特定语言不使用相同的表示形式,那么在移植时是否会因可移植性原因而导致问题?
另外,由于不理解 0x01f 问题,我可能读错了,但这不是只是根据最低有效位将红色、绿色和蓝色表示设置为 0 或 1 吗?
I'm looking at the original source code for Identicons.
There's a bit of code that does some bit twiddling to extract red, green and blue components:
int blue = (code >> 16) & 0x01f;
int green = (code >> 21) & 0x01f;
int red = (code >> 27) & 0x01f;
The code variable is a 32 bit integer.
My question is this: What's the difference between the number 0x01 and 0x01f?
I'm assuming the f means the value is a float, but why make it a float? Is it that the float representation in binary is different to the integer representation? Wouldn't that cause issues for portability reasons when porting if a particular language doesn't use the same representation?
Also, I'm probably reading this wrong on account of not understanding the 0x01f issue, but isn't this just setting the red, green and blue representations to either 0 or 1, depending on the least significant bit?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这与浮动无关。那是一个位掩码。
当您使用它
&
您的数字时,它会清除从右侧第 5 位左侧的所有位。以下是
0x0a2bfb & 0x01f=> 0x00001b:
That has nothing to do with floats. That's a bitmask.
When you
&
your number with it, it clears all the bits to left of the 5th bit from the right.Here's how
0x0a2bfb & 0x01f => 0x00001b
:我不确定Java,但在C99中,十六进制浮点数确实存在< /a>,它们看起来像这样:
p
后缀是强制性的(用于指示 2 的幂)。因此,像0x01f
这样的十六进制整数字面值不会产生歧义。编译器不会将f
误认为浮点常量后缀,它会将其读取为十六进制数字。I'm not sure about Java, but in C99, hex floats really do exist, and they look like this:
The
p
suffix is mandatory (and is used to indicate the power of 2 to raise the number). Hence, there is no ambiguity with hex integer literals like0x01f
. The compiler won't mistakef
for a floating point constant suffix, it'll read it as a hex digit.在十六进制上下文中,
f
不是浮点数。0xf
相当于十进制的15
。在您的示例中,代码正在查找颜色代码的最低有效 5 位,可能在 15 或 16 位颜色空间中
f
in the context of hexadecimal is not a float.0xf
is equivalent to decimal15
.In your example, the code is finding the least significant 5 bits of the color code, likely in a 15 or 16bit color space