1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package com.vcollaborate.bitwise;
21
22 import com.vcollaborate.math.CombinatoricUtils;
23
24 import java.util.BitSet;
25
26 public final class BinaryUtils {
27
28 private BinaryUtils() {
29 }
30
31
32
33
34 public static final BitSet toBitSet(final int size, final long value) {
35 BitSet bits = new BitSet(size);
36 int idx = 0;
37 long tmp = value;
38 while (tmp != 0L) {
39 if (tmp % 2L != 0L) {
40 bits.set(idx);
41 }
42 ++idx;
43 tmp = tmp >>> 1;
44 }
45 return bits;
46 }
47
48
49
50
51
52
53 public static final long fromBitSet(final BitSet bits) {
54 long value = 0L;
55 for (int i = 0; i < bits.size(); i++) {
56 value += bits.get(i) ? (1L << i) : 0L;
57 }
58 return value;
59 }
60
61
62
63
64
65
66
67
68
69 public static final long nextPermutation(long val) {
70 long tmp = val | (val - 1);
71 return (tmp + 1) | (((-tmp & -~tmp) - 1) >> (Long.numberOfTrailingZeros(val) + 1));
72 }
73
74
75
76
77
78 public static final long[] getAllPermutations(int bits, int length) {
79 long min = allOnes(bits);
80 long permutations = CombinatoricUtils.combinations(length, bits);
81 long[] retVal = new long[(int) permutations];
82
83 long val = min;
84 for (int idx = 0; idx < permutations; idx++) {
85 retVal[idx] = val;
86 val = nextPermutation(val);
87 }
88 return retVal;
89 }
90
91
92
93
94 public static final int getHammingDistance(final long val1, final long val2) {
95 return Long.bitCount(val1 ^ val2);
96 }
97
98
99
100
101 public static final long allOnes(final long length) {
102 return (1 << length) - 1;
103 }
104
105
106
107
108 public static final boolean isBitSet(final long val, final int pos) {
109 return (val & (1 << pos)) != 0;
110 }
111
112
113
114
115 public static final int[] getBitsSet(final long val) {
116 long tmp = val;
117 int[] retVal = new int[Long.bitCount(val)];
118 for (int i = 0; i < retVal.length; i++) {
119 retVal[i] = Long.numberOfTrailingZeros(tmp);
120 tmp = tmp ^ Long.lowestOneBit(tmp);
121 }
122 return retVal;
123 }
124 }