[英]In Java, is it possible to clear a bit?
在 Java 中,是否可以使用按位運算清除一點?
dfa 答案的替代方法是使用BitSet 數據結構。 它支持AND、OR 和XOR 等操作。
var bitset = new BitSet(K); // Initialize a bitset with K digits.
var nthBit = bitset.get(n); // Get Nth bit (true/false).
bitset.clear(n); // Clears N'th bit to false.
bitset.set(n); // Sets N'th bit to true.
bitset.flip(n); // Toggles N'th bit.
在您的情況下,您將尋找bitset.clear(n)
。
如果您必須使用整數,請使用bits |= (1 << N)
設置值,並如 dfa 所述,使用以下方法清除: bits &= ~(1 << N)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.