-
Notifications
You must be signed in to change notification settings - Fork 1
/
BinaryUtils.java
124 lines (110 loc) · 3.63 KB
/
BinaryUtils.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
* Copyright (C) 2012-2015 Christian Sterzl <christian.sterzl@gmail.com>
*
* This file is part of Bittwiddling.
*
* Bittwiddling is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Bittwiddling is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Bittwiddling. If not, see <http://www.gnu.org/licenses/>.
*/
package com.vcollaborate.bitwise;
import com.vcollaborate.math.CombinatoricUtils;
import java.util.BitSet;
public final class BinaryUtils {
private BinaryUtils() {
}
/**
* Converts {@code value} into a {@link BitSet} of size {@code size}.
*/
public static final BitSet toBitSet(final int size, final long value) {
BitSet bits = new BitSet(size);
int idx = 0;
long tmp = value;
while (tmp != 0L) {
if (tmp % 2L != 0L) {
bits.set(idx);
}
++idx;
tmp = tmp >>> 1;
}
return bits;
}
/**
* Converts the {@link BitSet} {@code bits} into a long value.
* <p>
* This is the reverse operation from {@link #toBitSet(int, long)}.
*/
public static final long fromBitSet(final BitSet bits) {
long value = 0L;
for (int i = 0; i < bits.size(); i++) {
value += bits.get(i) ? (1L << i) : 0L;
}
return value;
}
/**
* Compute the lexicographically next bit permutation.
*
* Suppose we have a pattern of N bits set to 1 in an integer and we want the next permutation of
* N 1 bits in a lexicographical sense. For example, if N is 3 and the bit pattern is 00010011,
* the next patterns would be 00010101, 00010110, 00011001,00011010, 00011100, 00100011, and so
* forth.
*/
public static final long nextPermutation(long val) {
long tmp = val | (val - 1);
return (tmp + 1) | (((-tmp & -~tmp) - 1) >> (Long.numberOfTrailingZeros(val) + 1));
}
/**
* Returns an array containing all permutations of a defined bitset with {@code bits} set and
* maximum length of {@code length}.
*/
public static final long[] getAllPermutations(int bits, int length) {
long min = allOnes(bits);
long permutations = CombinatoricUtils.combinations(length, bits);
long[] retVal = new long[(int) permutations];
long val = min;
for (int idx = 0; idx < permutations; idx++) {
retVal[idx] = val;
val = nextPermutation(val);
}
return retVal;
}
/**
* Calculates the hamming distance between the two given values.
*/
public static final int getHammingDistance(final long val1, final long val2) {
return Long.bitCount(val1 ^ val2);
}
/**
* Returns a value with all bits set to 1 of length {@code length}.
*/
public static final long allOnes(final long length) {
return (1 << length) - 1;
}
/**
* Determines if the bit at position {@code pos} in the value {@code val} is set.
*/
public static final boolean isBitSet(final long val, final int pos) {
return (val & (1 << pos)) != 0;
}
/**
* Returns the number of bits set.
*/
public static final int[] getBitsSet(final long val) {
long tmp = val;
int[] retVal = new int[Long.bitCount(val)];
for (int i = 0; i < retVal.length; i++) {
retVal[i] = Long.numberOfTrailingZeros(tmp);
tmp = tmp ^ Long.lowestOneBit(tmp);
}
return retVal;
}
}