Skip to content

Commit eacf539

Browse files
committed
Add switch to set and clear bit
1 parent 86152ec commit eacf539

File tree

3 files changed

+20
-5
lines changed

3 files changed

+20
-5
lines changed

src/main/java/at/favre/lib/bytes/Bytes.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ public Bytes resize(int newByteLength) {
690690
}
691691

692692
/**
693-
* Returns a Byte whose value is equivalent to this Byte with the designated bit switched to newBitValue. Bits start to count from the LSB (ie. Bytes.from(0).switchBit(0,true) == 1)
693+
* Returns a Byte whose value is equivalent to this Byte with the designated bit set to newBitValue. Bits start to count from the LSB (ie. Bytes.from(0).switchBit(0,true) == 1)
694694
*
695695
* @param bitPosition not to confuse with byte position
696696
* @param newBitValue if true set to 1, 0 otherwise
@@ -700,6 +700,16 @@ public Bytes switchBit(int bitPosition, boolean newBitValue) {
700700
return transform(new BytesTransformer.BitSwitchTransformer(bitPosition, newBitValue));
701701
}
702702

703+
/**
704+
* Returns a Byte whose value is equivalent to this Byte with the designated bit switched.
705+
*
706+
* @param bitPosition not to confuse with byte position
707+
* @return instance with bit switched
708+
*/
709+
public Bytes switchBit(int bitPosition) {
710+
return transform(new BytesTransformer.BitSwitchTransformer(bitPosition, null));
711+
}
712+
703713
/**
704714
* Generic transformation of this instance.
705715
* <p>

src/main/java/at/favre/lib/bytes/BytesTransformer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,9 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
290290
*/
291291
class BitSwitchTransformer implements BytesTransformer {
292292
private final int position;
293-
private final boolean newBitValue;
293+
private final Boolean newBitValue;
294294

295-
BitSwitchTransformer(int position, boolean newBitValue) {
295+
BitSwitchTransformer(int position, Boolean newBitValue) {
296296
this.position = position;
297297
this.newBitValue = newBitValue;
298298
}
@@ -307,8 +307,10 @@ public byte[] transform(byte[] currentArray, boolean inPlace) {
307307

308308

309309
int bytePosition = currentArray.length - 1 - position / 8;
310-
if (newBitValue) {
310+
if (newBitValue == null) {
311311
out[bytePosition] ^= (1 << position % 8);
312+
} else if (newBitValue) {
313+
out[bytePosition] |= (1 << position % 8);
312314
} else {
313315
out[bytePosition] &= ~(1 << position % 8);
314316
}

src/test/java/at/favre/lib/bytes/BytesTransformTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,11 @@ public void bitSwitch() throws Exception {
249249
for (int i = 0; i < 63; i++) {
250250
for (long j = 1; j < 33; j++) {
251251
assertEquals("bit position " + i + " is wrong",
252-
BigInteger.valueOf(j).flipBit(i).longValue(),
252+
BigInteger.valueOf(j).setBit(i).longValue(),
253253
Bytes.from(j).switchBit(i, true).toLong());
254+
assertEquals("bit position " + i + " is wrong",
255+
BigInteger.valueOf(j).flipBit(i).longValue(),
256+
Bytes.from(j).switchBit(i).toLong());
254257
assertEquals("bit position " + i + " is wrong",
255258
BigInteger.valueOf(j).clearBit(i).longValue(),
256259
Bytes.from(j).switchBit(i, false).toLong());

0 commit comments

Comments
 (0)