说明
- java.util.BitSet实现了一个位向量,可以根据需要增长。
- BitSet中的每一位有一个布尔值。如果该位为1,那么是true;如果该位是0,那么是false。
- BitSet中的位编号从0开始。
- BitSet中的每一位都可以检查、可以设置为1、可以设置为0。
- 一个BitSet可以和另外一个BitSet进行逻辑与、或、亦或操作。
- 默认地,BitSet中的每一位初始值都是false。
使用set(int bitIndex)设置某位为true,并将结果打印出来
使用BitSet的set(int bitIndex)函数,将BitSet中索引为bitIndex的位设置为true。
使用BitSet的toString()函数,将BitSet中值为true的位的索引打印出来。
package com.thb;
import java.util.BitSet;
public class Demo7 {
public static void main(String[] args) {
BitSet bitSet = new BitSet();
// 此时打印的结果为{}
System.out.println(bitSet.toString());
// 设置索引为2的bit为true
bitSet.set(2);
// 此时打印的结果为{2}
System.out.println(bitSet.toString());
// 设置索引为0的bit为true
bitSet.set(0);
// 此时打印的结果为{0, 2}
System.out.println(bitSet.toString());
}
}
运行输出:
{}
{2}
{0, 2}
使用cardinality()返回BitSet中值为true的位的个数
使用BitSet的cardinality()函数,返回BitSet中值为true的位的个数。
package com.thb;
import java.util.BitSet;
public class Demo7 {
public static void main(String[] args) {
BitSet bitSet = new BitSet();
// 设置索引为2的bit为true
bitSet.set(2);
System.out.println("BitSet中值为true的位的个数:" + bitSet.cardinality());
// 设置索引为5的bit为true
bitSet.set(5);
System.out.println("BitSet中值为true的位的个数:" + bitSet.cardinality());
}
}
运行输出:
BitSet中值为true的位的个数:1
BitSet中值为true的位的个数:2
使用get(int bitIndex)获取BitSet中某一位的值
使用BitSet的get(int bitIndex)函数,获取BitSet中某一位的值。
package com.thb;
import java.util.BitSet;
public class Demo7 {
public static void main(String[] args) {
BitSet bitSet = new BitSet();
// 设置索引为2的位的值为true
bitSet.set(2);
// 取索引为2的位的值,应该为true
System.out.println(bitSet.get(2));
// 取索引为0的位的值,应该为false
System.out.println(bitSet.get(0));
}
}
运行输出:
true
false
使用isEmpty()判断BitSet中是否不包含值为true的位
使用BitSet的isEmpty()函数,判断BitSet中是否不包含值为true的位。
package com.thb;
import java.util.BitSet;
public class Demo7 {
public static void main(String[] args) {
BitSet bitSet = new BitSet();
// 判断BitSet中不包含值为true的位,应该为true
System.out.println(bitSet.isEmpty());
// 设置索引为2的位的值为true
bitSet.set(2);
// 判断BitSet中不包含值为true的位,应该为false
System.out.println(bitSet.isEmpty());
}
}
运行输出:
true
false
使用length()判断BitSet的长度
BitSet的length()函数,返回BitSet的“逻辑长度”。这个逻辑长度的值等于BitSet中设置为true的位的最大索引+1。
例如:
package com.thb;
import java.util.BitSet;
public class Demo7 {
public static void main(String[] args) {
BitSet bitSet = new BitSet();
// 设置索引为2的位的值为true
bitSet.set(2);
// 设置索引为10的位的值为true
bitSet.set(10);
// 判断BitSet的长度
System.out.println(bitSet.length());
}
}
运行输出:
使用size()返回BitSet使用了多少位来表示值
例如:
package com.thb;
import java.util.BitSet;
public class Demo7 {
public static void main(String[] args) {
BitSet bitSet = new BitSet();
// 设置索引为2的位的值为true
bitSet.set(2);
// 判断BitSet使用了多少位来表示值
System.out.println(bitSet.size());
// 设置索引为30的位的值为true
bitSet.set(30);
// 判断BitSet使用了多少位来表示值
System.out.println(bitSet.size());
// 设置索引为63的位的值为true
bitSet.set(63);
// 判断BitSet使用了多少位来表示值
System.out.println(bitSet.size());
// 设置索引为63的位的值为true
bitSet.set(64);
// 判断BitSet使用了多少位来表示值
System.out.println(bitSet.size());
}
}
运行输出:
64
64
64
128
从输出可以看出,BitSet每次扩展64位。
使用toByteArray()返回字节数组,存放BitSet的位
toByteArray()返回字节数组其实是以BitSet中设置为true的位的最大索引来判断的。
例如:
如果设置为true的位的最大索引是2,那么其实只有3位充分利用了,用一个字节就行,所以返回1个字节;
如果设置为true的位的最大索引是8,那么其实只有9位充分利用了,用2个字节就行,所以返回2个字节。
代码示例:
package com.thb;
import java.util.BitSet;
public class Demo7 {
public static void main(String[] args) {
BitSet bitSet = new BitSet();
byte[] bytes;
// 设置索引为2的位的值为true
bitSet.set(2);
System.out.println("BitSet长度 = " + bitSet.length());
bytes = bitSet.toByteArray();
System.out.println("字节数: " + bytes.length);
// 设置索引为7的位的值为true
bitSet.set(7);
System.out.println("BitSet长度 = " + bitSet.length());
bytes = bitSet.toByteArray();
System.out.println("字节数: " + bytes.length);
// 设置索引为8的位的值为true
bitSet.set(8);
System.out.println("BitSet长度 = " + bitSet.length());
bytes = bitSet.toByteArray();
System.out.println("字节数: " + bytes.length);
}
}
运行输出:
BitSet长度 = 3
字节数: 1
BitSet长度 = 8
字节数: 1
BitSet长度 = 9
字节数: 2