Java 对整数变量使用 0x01 而不是 1 的优势?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6533582/
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
Advantage of using 0x01 instead of 1 for an integer variable?
提问by Ebbu Abraham
Recently I came across a line like this
最近我遇到了这样的一行
public final static int DELETION_MASK = 0x01;
公共最终静态 int DELETION_MASK = 0x01;
why is it not like
为什么不像
public final static int DELETION_MASK = 1;
公共最终静态 int DELETION_MASK = 1;
Is there any advantage in using the first approach other than 0xA and upper limit hexadecimals can be converted with ease?? In this case its just a constant representing 1.
除了 0xA 和上限十六进制可以轻松转换之外,使用第一种方法有什么好处吗??在这种情况下,它只是一个代表 1 的常数。
采纳答案by thkala
While there is not a difference in the code produced by the compiler, bit masks are traditionally written using the hexadecimal notation, because it's significantly easier for a human to convert to a binary form. Another common convention is to include the leading zeros when the length of the field is known. E.g. for a C int
field, it's common to write:
虽然编译器生成的代码没有区别,但位掩码传统上是使用十六进制表示法编写的,因为人类将其转换为二进制形式要容易得多。另一个常见的约定是在字段长度已知时包含前导零。例如,对于 Cint
字段,通常这样写:
#define MASK 0x0000ffff
In addition, hexadecimal constants indicate to the programmer that it's probablya bit mask, or a value that will be somehow involved in bitwise operations and should probably be treated specially.
此外,十六进制常量向程序员表明它可能是位掩码,或者以某种方式涉及按位运算的值,可能应该被特殊对待。
As a bonus, hexadecimal notations may also avoid issues with negative numbers: 0xffffffff
is in fact a negative number (-1
to be exact). Rather than juggling with the sign and 2's-complement numbers you can just specify the mask in hexadecimal and be done with it.
作为奖励,十六进制表示法还可以避免负数问题:0xffffffff
实际上是负数(-1
准确地说)。无需处理符号和 2 的补码,您只需指定十六进制掩码即可完成。
Since Java 7 you can also use binary literalswhich makes it even easier for a human to understand which bits are set in a bit mask. And binary literals may make use of underscoresto put the bits into separate groups.
从 Java 7 开始,您还可以使用二进制文字,这使人们更容易理解位掩码中设置了哪些位。并且二进制文字可以使用下划线将位放入单独的组中。
That means that the following is also valid:
这意味着以下内容也有效:
public final static int DELETION_MASK = 0b0000_0001;
回答by SJuan76
Only that, it will be consistent when you define NONDELETION_MASK = 0x0A.
只有这样,当您定义 NONDELETION_MASK = 0x0A 时才会一致。
回答by Kamahire
It is easy to understand. Whenever we think about masking then we always think in HEX or BIN numbers.
这很容易理解。每当我们考虑屏蔽时,我们总是会想到 HEX 或 BIN 数字。
回答by Rob Agar
It helps with the mental conversion between the integer value and the bit pattern it represents, which is the thing that matters for flags and masks.
它有助于整数值和它所代表的位模式之间的心理转换,这对标志和掩码很重要。
Because 16 is a power of 2 (unlike 10), you get nice repeating things like this:
因为 16 是 2 的幂(与 10 不同),所以你可以很好地重复这样的事情:
public final static int A_FLAG = 0x01; // 00000001
public final static int B_FLAG = 0x02; // 00000010
public final static int C_FLAG = 0x04; // 00000100
public final static int D_FLAG = 0x08; // 00001000
public final static int E_FLAG = 0x10; // 00010000
public final static int F_FLAG = 0x20; // 00100000
public final static int G_FLAG = 0x40; // 01000000
public final static int H_FLAG = 0x80; // 10000000