选择常量的值

发布于 2024-09-14 23:59:59 字数 328 浏览 5 评论 0原文

我从未真正理解的一件事是,为什么在许多库中,常量是这样定义的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒夜笙歌凉 2024-09-21 23:59:59

1 的位移位通常适用于您想要存储非独占值的情况。

例如,假设您希望能够在盒子的任意一侧绘制线条。您定义:

LEFT_SIDE   = 1 << 0  # binary 0001 (1)
RIGHT_SIDE  = 1 << 1  # binary 0010 (2)
TOP_SIDE    = 1 << 2  # binary 0100 (4)
BOTTOM_SIDE = 1 << 3  # binary 1000 (8)
                               ----
                               0111 (7) = LEFT_SIDE | RIGHT_SIDE | TOP_SIDE

然后您可以将它们组合为多个侧面:

DrawBox (LEFT_SIDE | RIGHT_SIDE | TOP_SIDE) # Don't draw line on bottom.

它们使用完全不同的位这一事实意味着它们彼此独立。通过对它们进行“或”运算,您将得到 1 | 2 | 4 等于 7,您可以使用其他布尔运算检测每个单独的位(请参阅 此处此处 有关这些的解释)。

如果它们被定义为 1、2、3 和 4,那么您可能必须为每一侧调用一次,或者必须传递四个不同的参数,每侧一个。否则,您无法区分 LEFT 和 RIGHT (1 + 2 = 3) 和 TOP (3 >),因为它们都是相同的值(通过简单的加法运算)。

0x 内容只是十六进制数字,更容易将其视为二进制位掩码(每个十六进制数字与四个二进制数字完全对应。您往往会看到类似 0x01 的模式, 0x020x040x080x100x20 等等,因为它们相当于单个 1 位移向最高有效位位置 - 这些值相当于二进制 000000010000001000000100000010000001000000100000 等等。

旁白:一旦你习惯了十六进制,你就很少需要担心1 << n 东西。您可以立即将 0x4000 识别为二进制 0100 0000 0000 0000。如果您在代码中看到值 16384,那就不太明显了,尽管我们中的一些人甚至认识到这一点:-)


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:

LEFT_SIDE   = 1 << 0  # binary 0001 (1)
RIGHT_SIDE  = 1 << 1  # binary 0010 (2)
TOP_SIDE    = 1 << 2  # binary 0100 (4)
BOTTOM_SIDE = 1 << 3  # binary 1000 (8)
                               ----
                               0111 (7) = LEFT_SIDE | RIGHT_SIDE | TOP_SIDE

Then you can combine them for multiple sides:

DrawBox (LEFT_SIDE | RIGHT_SIDE | TOP_SIDE) # Don't draw line on bottom.

The fact that they're using totally different bits means that they're independent of each other. By ORing them you get 1 | 2 | 4 which is equal to 7 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) and TOP (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 like 0x01, 0x02, 0x04, 0x08, 0x10, 0x20 and so on, since they're the equivalent of a single 1 bit moving towards the most significant bit position - those values are equivalent to binary 00000001, 00000010, 00000100, 00001000, 00010000, 00100000 and so on.

Aside: Once you get used to hex, you rarely have to worry about the 1 << n stuff. You can instantly recognise 0x4000 as binary 0100 0000 0000 0000. That's less obvious if you see the value 16384 in the code although some of us even recognise that :-)

狼性发作 2024-09-21 23:59:59

关于 << 的东西:这是我喜欢的方式。

当我需要定义常量时,第 2 位位置为 1,所有其他位为 0,我可以将其定义为 40x4111 。 <2。在我看来, 1<<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 or 1<<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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文