以下代码从一个长值创建一个位集,反之亦然:
public class Bits { public static BitSet convert(long value) { BitSet bits = new BitSet(); int index = 0; while (value != 0L) { if (value % 2L != 0) { bits.set(index); } ++index; value = value >>> 1; } return bits; } public static long convert(BitSet bits) { long value = 0L; for (int i = 0; i < bits.length(); ++i) { value += bits.get(i) ? (1L << i) : 0L; } return value; }}编辑:现在两个方向,@leftbrain:原因,你是对的



