當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java BitSet andNot()用法及代碼示例

BitSet類andNot()方法

  • andNot() 方法在 java.util 包中可用。
  • andNot() 方法用於返回此 Bitset 中其對應位未在給定 Bitset 中設置的所有位。
  • andNot() 方法是一個非靜態方法,因此可以通過類對象訪問它,如果我們嘗試使用類名訪問該方法,則會出現錯誤。
  • andNot() 方法在清除此 Bitset 中的位時不引發異常,其對應位出現在給定的 Bitset 中。

用法:

    public void andNot(BitSet bs);

參數:

  • BitSet bs– 表示用於屏蔽此 Bitset 的 Bitset。

返回值:

這個方法的返回類型是void,它什麽都不返回。

例:

// Java program to demonstrate the example 
// of void andNot(BitSet bs) method of BitSet.

import java.util.*;

public class AndNotOfBitSet {
    public static void main(String[] args) {
        // create an object of two BitSet
        BitSet bs1 = new BitSet(10);
        BitSet bs2 = new BitSet(10);

        // By using set() method is to set
        // the values in BitSet 1 
        bs1.set(10);
        bs1.set(20);
        bs1.set(30);
        bs1.set(40);
        bs1.set(50);

        // By using set() method is to set
        // the values in BitSet 2 
        bs2.set(60);
        bs2.set(70);
        bs2.set(50);
        bs2.set(40);
        bs2.set(30);

        // Display Bitset1 and BitSet2
        System.out.println("bs1:" + bs1);
        System.out.println("bs2:" + bs2);

        // By using andNot(BitSet) method is to returns
        // all the bits that does not match the bits of given
        // BitSet

        bs1.andNot(bs2);

        // Display BitSet 1
        System.out.println("bs1.andNot(bs2):" + bs1);
    }
}

輸出

bs1:{10, 20, 30, 40, 50}
bs2:{30, 40, 50, 60, 70}
bs1.andNot(bs2):{10, 20}


相關用法


注:本文由純淨天空篩選整理自Preeti Jain大神的英文原創作品 Java BitSet andNot() Method with Example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。