testbits.java

来自「国外的数据结构与算法分析用书」· Java 代码 · 共 48 行

JAVA
48
字号
import java.io.*;
import java.util.BitSet;

public class TestBits
{
	public TestBits()
	{
		BitSet a = new BitSet(10);
		BitSet b = new BitSet(10);
		BitSet c = new BitSet(10);
		
		a.set(0); a.set(2); a.set(6); a.set(9);
		b.set(0); b.set(1); b.set(2); b.set(7); b.set(8); b.set(9);

		c = (BitSet)a.clone();
		a.or(b);
		if (a.get(1))
			System.out.println("1 belongs to the union of a and b");
		else
			System.out.println("1 does not belong to the union of a and b");
	
		a = (BitSet)c.clone();
		a.and(b);
		if (a.get(2))
			System.out.println("2 belongs to the intersection of a and b");
		else
			System.out.println("2 does not belong to the intersection of a and b");

		a = (BitSet)c.clone();
		a.andNot(b);
		if (a.get(7))
			System.out.println("7 belongs to the set difference of a and b");
		else
			System.out.println("7 does not belong to the set difference of a and b");
			
		BitSet bComplement = (BitSet)b.clone();
		bComplement.flip(0, 10);
		System.out.println("b's complement is " + bComplement.toString());
		
	}

	public static void main(String args[])
	{
		TestBits tb = new TestBits();
	}

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?