[英]Bitwise operators in java
我從Picasso's
源代碼中復制了以下類。 實際上,我要求Picasso
不要緩存圖像。
任何人都可以向我解釋這兩行
NO_CACHE(1 << 0),
NO_STORE(1 << 1);
我知道按位運算符,我只想知道為什么我們需要它們?
他們還壓制了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值)。
因此,您的問題的答案是:不需要這些轉換操作! 它們沒有額外的價值(更糟糕的是:它們似乎讓讀者感到困惑)
潛在的想法可能是在稍后的某個時候會發生某種“比特掩蔽”。 而且你知道,當你后來用“位”來思考時,有人在宣布這些常數時也有一個很好的想法“在位中思考”。
但在那種情況下,像
NO_CACHE(0b01), NO_STORE(0b10);
我也會做很多工作。 但即便如此; 我覺得沒用。 如果有的話,我寧願在那里放一個javadoc,表示這些常量后來打算用作“按位標志”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.