当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java BitSet or()用法及代码示例


先决条件: Java BitSet |套装1

BitSet是java.util包中定义的类。它创建一个由布尔值表示的位数组。它执行调用方BitSet与被调用BitSet的逻辑或。仅当它为true且BitSet参数的值为true时,才会设置此BitSet。

用法:


public void and(BitSet set);

例子:

Input:
set1:{1, 2, 4}
set2:{2, 3, 4}

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

程序:

// Java program illustrating Bitset Class or() function. 
import java.util.*; 
public class GFG { 
    public static void main(String[] args) 
    { 
        // Constructors of BitSet class 
        BitSet set1 = new BitSet(); 
        BitSet set2 = new BitSet(6); 
  
        /* set is BitSet class method 
        expalined in next articles */
        set1.set(21); 
        set1.set(45); 
        set1.set(8); 
        set1.set(23); 
  
        // assign values to set2 
        set2.set(12); 
        set2.set(89); 
        set2.set(21); 
        set2.set(78); 
        set2.set(93); 
        set2.set(3); 
  
        // Printing the 2 Bitsets 
        System.out.println("set1:" + set1); 
        System.out.println("set2:" + set2); 
  
        // Performing logical AND 
        // on set2 set with set1 
        set2.or(set1); 
  
        // set2 set after Performing AND 
        System.out.println("After Performing OR:"); 
        System.out.println(set2); 
    } 
}
输出:
set1:{8, 21, 23, 45}
set2:{3, 12, 21, 78, 89, 93}
After Performing OR:
{3, 8, 12, 21, 23, 45, 78, 89, 93}

相关文章:

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


相关用法


注:本文由纯净天空筛选整理自barykrg大神的英文原创作品 Java BitSet | or()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。