Java BitSet andNot() 方法



描述

Java BitSet andNot(BitSet set) 方法清除此 BitSet 中所有其对应位在指定 BitSet 中已设置的位。

声明

以下是 java.util.BitSet.andNot() 方法的声明

public void andNot(BitSet set)

参数

set − 用于屏蔽此 BitSet 的 BitSet。

返回值

此方法不返回值。

异常

对 BitSet 执行 And Not 操作示例

以下示例演示了 Java BitSet andNot() 方法的用法。我们创建了两个 BitSet。我们使用 set() 方法根据索引在 BitSet 对象中设置 true 值,并使用 andNot() 方法执行andNot 操作并打印更新后的 bitset。

package com.tutorialspoint;

import java.util.BitSet;

public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = new BitSet();
      BitSet bitset2 = new BitSet();

      // assign values to bitset1
      bitset1.set(0, 6, true);

      // assign values to bitset2
      bitset2.set(2);
      bitset2.set(4);
      bitset2.set(6);
      bitset2.set(8);
      bitset2.set(10);

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);

      // perform andNot operation between two bitsets
      bitset1.andNot(bitset2);

      // print the new bitset1
      System.out.println(bitset1);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Bitset1:{0, 1, 2, 3, 4, 5}
Bitset2:{2, 4, 6, 8, 10}
{0, 1, 3, 5}

对字节型 BitSet 执行 And Not 操作示例

以下示例演示了 Java BitSet andNot() 方法的用法。我们使用 byte[] 创建两个 BitSet,并使用 andNot() 方法执行andNot 操作并打印更新后的 bitset。

package com.tutorialspoint;

import java.util.BitSet;

public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = BitSet.valueOf(new byte[] { 0, 1, 2, 3, 4, 5 });
      BitSet bitset2 = BitSet.valueOf(new byte[] { 2, 4, 6, 8, 10 });

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);

      // perform andNot operation between two bitsets
      bitset1.andNot(bitset2);

      // print the new bitset1
      System.out.println(bitset1);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Bitset1:{8, 17, 24, 25, 34, 40, 42}
Bitset2:{1, 10, 17, 18, 27, 33, 35}
{8, 24, 25, 34, 40, 42}

对长整型 BitSet 执行 And Not 操作示例

以下示例演示了 Java BitSet andNot() 方法的用法。我们使用 long[] 创建两个 BitSet,并使用 andNot() 方法执行andNot 操作并打印更新后的 bitset。

package com.tutorialspoint;

import java.util.BitSet;

public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = BitSet.valueOf(new long[] { 0, 1, 2, 3, 4, 5 });
      BitSet bitset2 = BitSet.valueOf(new long[] { 2, 4, 6, 8, 10 });

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);

      // perform andNot operation between two bitsets
      bitset1.andNot(bitset2);

      // print the new bitset1
      System.out.println(bitset1);
   }
}

输出

让我们编译并运行上述程序,这将产生以下结果:

Bitset1:{64, 129, 192, 193, 258, 320, 322}
Bitset2:{1, 66, 129, 130, 195, 257, 259}
{64, 192, 193, 258, 320, 322}
java_util_bitset.htm
广告
© . All rights reserved.