-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBit.java
More file actions
171 lines (152 loc) · 4.67 KB
/
Bit.java
File metadata and controls
171 lines (152 loc) · 4.67 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
// Owen Gregson
// Artificial Intelligence
// TTT Checkpoint #3
// Dec 18, 2024
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Bit {
/**
* Checks if the bit at the specified position is set.
*
* @param x The bitmask.
* @param pos The position to check (0-63).
* @return True if the bit at position `pos` is set; otherwise, false.
*/
public static boolean isSet(long x, int pos) {
return (x & (1L << pos)) != 0;
}
/**
* Sets the bit at the specified position.
*
* @param x The original bitmask.
* @param pos The position to set (0-63).
* @return A new bitmask with the bit at position `pos` set.
*/
public static long set(long x, int pos) {
return x | (1L << pos);
}
/**
* Clears the bit at the specified position.
*
* @param x The original bitmask.
* @param pos The position to clear (0-63).
* @return A new bitmask with the bit at position `pos` cleared.
*/
public static long clear(long x, int pos) {
return x & ~(1L << pos);
}
/**
* Returns a bitmask with only the bit at the specified position set.
*
* @param position The position to set (0-63).
* @return A bitmask with only the bit at position `position` set.
*/
public static long positionMask(int position) {
return mask(position);
}
/**
* Returns a bitmask with only the bit at the specified position set.
* Alias for positionMask to maintain consistency.
*
* @param position The position to set (0-63).
* @return A bitmask with only the bit at position `position` set.
*/
public static long mask(int position) {
return 1L << position;
}
/**
* Finds the position of the leading one in the bitmask.
*
* @param x The bitmask.
* @return The position of the leading one, or -1 if `x` is zero.
*/
public static int leadingOne(long x) {
// Position of the leading one (inverse of mask function).
// Note: returns -1 if x is zero.
return x == 0 ? -1 : 63 - countLeadingZeros(x);
}
/**
* Counts the number of set bits (1s) in the bitmask.
*
* @param x The bitmask.
* @return The number of set bits.
*/
public static int countOnes(long x) {
int count = 0;
while (x != 0) {
x &= (x - 1);
count++;
}
return count;
}
/**
* Counts the number of leading zeros in the bitmask.
*
* @param x The bitmask.
* @return The number of leading zeros.
*/
public static int countLeadingZeros(long x) {
int count = 0;
int shift = 32;
long y;
while (shift > 0) {
y = x >>> shift;
if (y != 0) {
count += shift;
x = y;
}
shift >>= 1;
}
return x == 0 ? 64 : count + Long.numberOfLeadingZeros(x);
}
/**
* Returns an iterator over the positions of set bits (1s) in the bitmask.
*
* @param bits The bitmask.
* @return An iterator over the positions of set bits.
*/
public static Iterator<Integer> iterator(long bits) {
return new BitIterator(bits);
}
/**
* Returns an iterable over the positions of set bits (1s) in the bitmask.
*
* @param bits The bitmask.
* @return An iterable over the positions of set bits.
*/
public static Iterable<Integer> ones(long bits) {
return () -> new BitIterator(bits);
}
/**
* Returns a list of positions where bits are set (1s) in the bitmask.
*
* @param bits The bitmask.
* @return A list of positions with set bits.
*/
public static List<Integer> onesList(long bits) {
List<Integer> list = new ArrayList<>();
for (int pos : ones(bits)) {
list.add(pos);
}
return list;
}
/**
* Private class to implement the Iterator for set bits.
*/
private static class BitIterator implements Iterator<Integer> {
private long bits;
public BitIterator(long bits) {
this.bits = bits;
}
public boolean hasNext() {
return this.bits != 0;
}
public Integer next() {
long temp = this.bits & (this.bits - 1); // Clear the least significant 1 bit
long mask = this.bits ^ temp; // Mask for the bit just cleared
this.bits = temp; // Update the iterator state
return leadingOne(mask); // Position of the bit just cleared
}
}
}