2022-07-20 13:18:57 +02:00
|
|
|
package fr.pandacube.lib.net;
|
2016-11-22 21:32:18 +01:00
|
|
|
|
|
|
|
import java.util.Arrays;
|
|
|
|
|
2022-07-20 13:18:57 +02:00
|
|
|
import com.google.common.annotations.Beta;
|
|
|
|
|
|
|
|
@Beta
|
2020-02-08 15:43:19 +01:00
|
|
|
public class Array8Bit {
|
2016-11-22 21:32:18 +01:00
|
|
|
|
2017-01-15 22:27:57 +01:00
|
|
|
public static final int BIT_COUNT = Byte.SIZE;
|
2016-11-22 21:32:18 +01:00
|
|
|
|
|
|
|
private boolean[] values = new boolean[BIT_COUNT];
|
|
|
|
|
2017-01-15 22:27:57 +01:00
|
|
|
public Array8Bit(byte b) {
|
|
|
|
fromByte(b);
|
|
|
|
}
|
|
|
|
|
2016-11-22 21:32:18 +01:00
|
|
|
/**
|
2017-01-15 22:27:57 +01:00
|
|
|
* @param bits (index 0 is the lowest significant bit)
|
2016-11-22 21:32:18 +01:00
|
|
|
*/
|
|
|
|
public Array8Bit(boolean[] bits) {
|
|
|
|
if (bits == null || bits.length != BIT_COUNT)
|
|
|
|
throw new IllegalArgumentException("bits is null or bits.length != "+BIT_COUNT);
|
|
|
|
values = Arrays.copyOf(bits, BIT_COUNT);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i = 0 is the lowest significant bit
|
|
|
|
*/
|
2017-01-15 22:27:57 +01:00
|
|
|
public boolean getBit(int i) {
|
2016-11-22 21:32:18 +01:00
|
|
|
return values[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* i = 0 is the lowest significant bit
|
|
|
|
*/
|
2017-01-15 22:27:57 +01:00
|
|
|
public void setBit(int i, boolean b) {
|
2016-11-22 21:32:18 +01:00
|
|
|
values[i] = b;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-01-15 22:27:57 +01:00
|
|
|
|
|
|
|
public void fromByte(byte b) {
|
|
|
|
int mask = 1;
|
|
|
|
for (int i = 0; i < BIT_COUNT; i++) {
|
|
|
|
values[i] = (b & mask) != 0;
|
|
|
|
mask <<= 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public byte toByte() {
|
2016-11-22 21:32:18 +01:00
|
|
|
byte b = 0;
|
|
|
|
for (int i=BIT_COUNT-1; i>=0; i--) {
|
|
|
|
b <<= 1;
|
|
|
|
if (values[i]) b |= 1;
|
|
|
|
}
|
|
|
|
return b;
|
|
|
|
}
|
|
|
|
|
2017-01-15 22:27:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2016-11-22 21:32:18 +01:00
|
|
|
}
|