选择常量的值
我从未真正理解的一件事是,为什么在许多库中,常量是这样定义的:
public static final int DM_FILL_BACKGROUND = 0x2;
public static final int DM_FILL_PREVIOUS = 0x3;
public static final int TRANSPARENCY_MASK = 1 << 1;
public static final int TRANSPARENCY_PIXEL = 1 << 2;
0x 和 << 是怎么回事?东西?为什么人们不只使用普通的整数值?
One thing I've never really understood is why in many libraries, constants are defined like this:
public static final int DM_FILL_BACKGROUND = 0x2;
public static final int DM_FILL_PREVIOUS = 0x3;
public static final int TRANSPARENCY_MASK = 1 << 1;
public static final int TRANSPARENCY_PIXEL = 1 << 2;
What's up with the 0x and << stuff? Why aren't people just using ordinary integer values?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
1
的位移位通常适用于您想要存储非独占值的情况。例如,假设您希望能够在盒子的任意一侧绘制线条。您定义:
然后您可以将它们组合为多个侧面:
它们使用完全不同的位这一事实意味着它们彼此独立。通过对它们进行“或”运算,您将得到
1 | 2 | 4
等于7
,您可以使用其他布尔运算检测每个单独的位(请参阅 此处和此处 有关这些的解释)。如果它们被定义为 1、2、3 和 4,那么您可能必须为每一侧调用一次,或者必须传递四个不同的参数,每侧一个。否则,您无法区分
LEFT 和 RIGHT
(1 + 2 = 3
) 和TOP
(3
>),因为它们都是相同的值(通过简单的加法运算)。0x
内容只是十六进制数字,更容易将其视为二进制位掩码(每个十六进制数字与四个二进制数字完全对应。您往往会看到类似0x01
的模式,0x02
、0x04
、0x08
、0x10
、0x20
等等,因为它们相当于单个1
位移向最高有效位位置 - 这些值相当于二进制00000001
、00000010
、00000100
、00001000
、00010000
、00100000
等等。The bit shifting of
1
is usually for situations where you have non-exclusive values that you want to store.For example, say you want to be able to draw lines on any side of a box. You define:
Then you can combine them for multiple sides:
The fact that they're using totally different bits means that they're independent of each other. By
OR
ing them you get1 | 2 | 4
which is equal to7
and you can detect each individual bit with other boolean operations (see here and here for an explanation of these).If they were defined as 1, 2, 3 and 4 then you'd probably either have to make one call for each side or you'd have to pass four different parameters, one per side. Otherwise you couldn't tell the difference between
LEFT and RIGHT
(1 + 2 = 3
) andTOP
(3
), since both of them would be the same value (with a simple addition operation).The
0x
stuff is just hexadecimal numbers which are easier to see as binary bitmasks (each hexadecimal digit corresponds exactly with four binary digits. You'll tend to see patterns like0x01
,0x02
,0x04
,0x08
,0x10
,0x20
and so on, since they're the equivalent of a single1
bit moving towards the most significant bit position - those values are equivalent to binary00000001
,00000010
,00000100
,00001000
,00010000
,00100000
and so on.关于
<<
的东西:这是我喜欢的方式。当我需要定义常量时,第 2 位位置为 1,所有其他位为 0,我可以将其定义为
4
、0x4
或1
1
。在我看来,1
。 <21<<2
更具可读性,并且准确地解释了该常量的用途。顺便说一句,所有这些方法都提供相同的性能,因为计算是在编译时完成的。
Regarding
<<
stuff: this in my preferred way.When I need to define the constant with 1 in the bit 2 position, and 0 in all other bits, I can define it as
4
,0x4
or1<<2
.1<<2
is more readable, to my opinion, and explains exactly the purpose of this constant.BTW, all these ways give the same performance, since calculations are done at compile time.