📜  Java.util.Bitset 类 |逻辑运算

📅  最后修改于: 2022-05-13 01:55:18.027000             🧑  作者: Mango

Java.util.Bitset 类 |逻辑运算

Bitset 类还允许对两个 Bitset 进行一些逻辑操作。支持的逻辑运算有:and、andNot、or、Xor。这些将在本文中讨论。

1. and(Bitset set) :此方法执行此目标位集与参数位集的逻辑与,并返回也存在于第二个位集中的第一个位集的值。

Declaration : 
   public void and(BitSet set)
Parameters : 
    set :  a bit set
Return Value : 
   This method does not return a value.   
// Java code to demonstrate the working
// of and(Bitset set) in Bitset
import java.util.*;
public class BitSetAnd {
  
public static void main(String[] args)
    {
  
        // Declaring 2 bitsets
        BitSet bset1 = new BitSet(5);
        BitSet bset2 = new BitSet(5);
  
        // adding the values to bset1
        // using set()
        bset1.set(0);
        bset1.set(1);
        bset1.set(2);
        bset1.set(3);
  
        // adding the values to bset2
        // using set()
        bset2.set(2);
        bset2.set(4);
        bset2.set(6);
        bset2.set(0);
  
        // printing the initial sets
        System.out.println("The elements of Bitset 1 are : " + bset1);
  
        System.out.println("The elements of Bitset 2 are : " + bset2);
  
        // perform "and" operation between two bitsets
        // using and()
        bset1.and(bset2);
  
        // printing the new bset1
        System.out.println("The resultant bset1 after and operation is : " + bset1);
    }
}

输出 :

The elements of Bitset 1 are : {0, 1, 2, 3}
The elements of Bitset 2 are : {0, 2, 4, 6}
The resultant bset1 after and operation is : {0, 2}

2. andNot(BitSet set) :此方法执行逻辑 NAND并返回参数 Bitset 中不存在的第一个 Bitset 的元素。

Declaration : 
   public void andNot(BitSet set)
Parameters : 
   set: the BitSet with which to mask this BitSet.
Return Value : 
   This method does not return a value.
// Java code to demonstrate the working
// of andNot(Bitset set) in Bitset
import java.util.*;
public class BitSetNotAnd {
  
public static void main(String[] args)
    {
  
        // Declaring 2 bitsets
        BitSet bset1 = new BitSet(5);
        BitSet bset2 = new BitSet(5);
  
        // adding the values to bset1
        // using set()
        bset1.set(0);
        bset1.set(1);
        bset1.set(2);
        bset1.set(3);
  
        // adding the values to bset2
        // using set()
        bset2.set(2);
        bset2.set(4);
        bset2.set(6);
        bset2.set(0);
  
        // printing the initial sets
        System.out.println("The elements of Bitset 1 are : " + bset1);
  
        System.out.println("The elements of Bitset 2 are : " + bset2);
  
        // perform "not-and" operation between two bitsets
        // using andNot()
        bset1.andNot(bset2);
  
        // printing the new bset1
        System.out.println("The resultant bset1 after andNot operation is : " + bset1);
    }
}

输出 :

The elements of Bitset 1 are : {0, 1, 2, 3}
The elements of Bitset 2 are : {0, 2, 4, 6}
The resultant bset1 after andNot operation is : {1, 3}

3. or(Bitset set) :此方法执行此目标位集与参数位集的逻辑或,并返回两个位集中存在的所有值,不返回重复元素。

Declaration : 
   public void or(BitSet set)
Parameters : 
    set :  a bit set
Return Value : 
   This method does not return a value.   
// Java code to demonstrate the working
// of or(Bitset set) in Bitset
import java.util.*;
public class BitSetOr {
  
public static void main(String[] args)
    {
  
        // Declaring 2 bitsets
        BitSet bset1 = new BitSet(5);
        BitSet bset2 = new BitSet(5);
  
        // adding the values to bset1
        // using set()
        bset1.set(0);
        bset1.set(1);
        bset1.set(2);
        bset1.set(3);
  
        // adding the values to bset2
        // using set()
        bset2.set(2);
        bset2.set(4);
        bset2.set(6);
        bset2.set(0);
  
        // printing the initial sets
        System.out.println("The elements of Bitset 1 are : " + bset1);
  
        System.out.println("The elements of Bitset 2 are : " + bset2);
  
        // perform "or" operation between two bitsets
        // using or()
        bset1. or (bset2);
  
        // printing the new bset1
        System.out.println("The resultant bset1 after or operation is : " + bset1);
    }
}

输出 :

The elements of Bitset 1 are : {0, 1, 2, 3}
The elements of Bitset 2 are : {0, 2, 4, 6}
The resultant bset1 after or operation is : {0, 1, 2, 3, 4, 6}

4. xor(BitSet set) :此方法执行逻辑异或并返回存在于一个位集中但不在另一个位集中的那些元素。

Declaration : 
   public void xor(BitSet set)
Parameters : 
    set a bit set.
Return Value : 
   This method does not return a value.
// Java code to demonstrate the working
// of xor(Bitset set) in Bitset
import java.util.*;
public class BitSetXor {
  
public static void main(String[] args)
    {
  
        // Declaring 2 bitsets
        BitSet bset1 = new BitSet(5);
        BitSet bset2 = new BitSet(5);
  
        // adding the values to bset1
        // using set()
        bset1.set(0);
        bset1.set(1);
        bset1.set(2);
        bset1.set(3);
  
        // adding the values to bset2
        // using set()
        bset2.set(2);
        bset2.set(4);
        bset2.set(6);
        bset2.set(0);
  
        // printing the initial sets
        System.out.println("The elements of Bitset 1 are : " + bset1);
  
        System.out.println("The elements of Bitset 2 are : " + bset2);
  
        // perform "xor" operation between two bitsets
        // using xor()
        bset1. xor (bset2);
  
        // printing the new bset1
        System.out.println("The resultant bset1 after xor operation is : " + bset1);
    }
}

输出 :

The elements of Bitset 1 are : {0, 1, 2, 3}
The elements of Bitset 2 are : {0, 2, 4, 6}
The resultant bset1 after xor operation is : {1, 3, 4, 6}