📜  Java位集 |和

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

Java位集 |和

先决条件: Java BitSet |设置 1
BitSet 是在Java.util 包中定义的一个类。它创建一个由布尔值表示的位数组。
执行此目标位集与参数位集的逻辑与。修改此位集,以便当且仅当它最初都具有值 true 并且位集参数中的相应位也具有值 true 时,其中的每个位都具有值 true。
句法

public void and(BitSet set);

例子:

Input : 
set1 : {1, 2, 4}
set2 : {2, 3, 4}
Output : After performing st1.and(set2)
set2 : {2, 4}

程序:

Java
// Java program illustrating Bitset Class constructors.
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // Constructors of BitSet class
        BitSet bs1 = new BitSet();
        BitSet bs2 = new BitSet(6);
 
        /* set is BitSet class method
        explained in next articles */
        bs1.set(0);
        bs1.set(1);
        bs1.set(2);
        bs1.set(4);
 
        // assign values to bs2
        bs2.set(4);
        bs2.set(6);
        bs2.set(5);
        bs2.set(1);
        bs2.set(2);
        bs2.set(3);
 
        // Printing the 2 Bitsets
        System.out.println("bs1 : " + bs1);
        System.out.println("bs2 : " + bs2);
 
        // Performing logical AND
        // on bs2 set with bs1
        bs2.and(bs1);
 
        // bs2 set after Performing AND
        System.out.println("\nAfter AND operation : ");
        System.out.println(bs2);
    }
}


输出:
bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6}

After AND operation : 
{1, 2, 4}

相关文章 :

  1. Bitset 类示例集 1
  2. Bitset 类示例集 2
  3. Bitset 类示例集 3