java 中的位运算符
Bitwise operators in java
我从 Picasso's
源代码中复制了以下 class。实际上,我是在要求 Picasso
不要缓存图像。
谁能给我解释一下这两行
NO_CACHE(1 << 0),
NO_STORE(1 << 1);
I know about bitwise operators, I just want to know why we need them
here?
他们还抑制了 PointlessBitwiseExpression
警告。
/** Designates the policy to use when dealing with memory cache. */
@SuppressWarnings("PointlessBitwiseExpression")
public enum MemoryPolicy {
/** Skips memory cache lookup when processing a request. */
NO_CACHE(1 << 0),
/**
* Skips storing the final result into memory cache. Useful for one-off requests
* to avoid evicting other bitmaps from the cache.
*/
NO_STORE(1 << 1);
static boolean shouldReadFromMemoryCache(int memoryPolicy) {
return (memoryPolicy & MemoryPolicy.NO_CACHE.index) == 0;
}
static boolean shouldWriteToMemoryCache(int memoryPolicy) {
return (memoryPolicy & MemoryPolicy.NO_STORE.index) == 0;
}
final int index;
private MemoryPolicy(int index) {
this.index = index;
}
}
你问我们为什么需要它们?这段代码归结为:
NO_CHACHE(1), NO_STORE(2);
就是这样(这里只是为了完整:那些常量声明只是调用采用 int 值的枚举的私有构造函数)。
因此,您问题的答案是:根本不需要这些移位操作!它们没有任何附加价值(更糟的是:它们似乎让读者感到困惑)
潜在的想法可能是稍后会发生某种 "bit masking"。你知道的,当你以后用 "bits" 来思考时,有人在声明这些常量时也有 "think in bits" 的好主意。
但在那种情况下,像
NO_CACHE(0b01), NO_STORE(0b10);
也能胜任这项工作。但即便如此;我会发现没有帮助。如果有的话,我宁愿在那里放一个 javadoc 来表示这些常量稍后将用作 "bitwise flags"。
我从 Picasso's
源代码中复制了以下 class。实际上,我是在要求 Picasso
不要缓存图像。
谁能给我解释一下这两行
NO_CACHE(1 << 0),
NO_STORE(1 << 1);
I know about bitwise operators, I just want to know why we need them here?
他们还抑制了 PointlessBitwiseExpression
警告。
/** Designates the policy to use when dealing with memory cache. */
@SuppressWarnings("PointlessBitwiseExpression")
public enum MemoryPolicy {
/** Skips memory cache lookup when processing a request. */
NO_CACHE(1 << 0),
/**
* Skips storing the final result into memory cache. Useful for one-off requests
* to avoid evicting other bitmaps from the cache.
*/
NO_STORE(1 << 1);
static boolean shouldReadFromMemoryCache(int memoryPolicy) {
return (memoryPolicy & MemoryPolicy.NO_CACHE.index) == 0;
}
static boolean shouldWriteToMemoryCache(int memoryPolicy) {
return (memoryPolicy & MemoryPolicy.NO_STORE.index) == 0;
}
final int index;
private MemoryPolicy(int index) {
this.index = index;
}
}
你问我们为什么需要它们?这段代码归结为:
NO_CHACHE(1), NO_STORE(2);
就是这样(这里只是为了完整:那些常量声明只是调用采用 int 值的枚举的私有构造函数)。
因此,您问题的答案是:根本不需要这些移位操作!它们没有任何附加价值(更糟的是:它们似乎让读者感到困惑)
潜在的想法可能是稍后会发生某种 "bit masking"。你知道的,当你以后用 "bits" 来思考时,有人在声明这些常量时也有 "think in bits" 的好主意。
但在那种情况下,像
NO_CACHE(0b01), NO_STORE(0b10);
也能胜任这项工作。但即便如此;我会发现没有帮助。如果有的话,我宁愿在那里放一个 javadoc 来表示这些常量稍后将用作 "bitwise flags"。