Something about bit operation
One day I was thinking of how to handle an array of bits so I can use it to mark some information of another array. After checking below thread final I've made my own class to handle it. As I am not a very good coder I may have made careless mistake but still want to put it here for my future reference and whoever passes by here please do leave me some comment about it so I can improve myself.
/**
* Create a simple class containing an array of bits.
*
* @author Whelan Chan
*
*/
public class BitArray{
private long bitX64[] = null;
private final static int BIT_SIZE = 64;
public BitArray(int size) {
bitX64 = new long[size / BIT_SIZE + (size % BIT_SIZE == 0 ? 0 : 1)];
}
public boolean getBit(int pos) {
return ((bitX64[pos / BIT_SIZE] >> pos & 1l) == 1l);
}
public void setBit(int pos, boolean b) {
long b8 = bitX64[pos / BIT_SIZE];
long posBit = (long) (1l << (pos % BIT_SIZE));
if (b) {
b8 |= posBit;
} else {
b8 &= ~posBit;
}
bitX64[pos / BIT_SIZE] = b8;
}
public long getLongValueOfAllBit(){
long ret = 0l;
for(int i=0; i ret |= bitX64[i];
}
return ret;
}
public boolean isAllFalse(){
return (this.getLongValueOfAllBit() == 0l);
}
public int getStorageArraySize(){
return bitX64.length;
}
public int getArraySize(){
return bitX64.length * BIT_SIZE;
}
}
Test cases:
@Test
public void testBitArraySize(){
BitArray bit10 = new BitArray(10);
BitArray bit127 = new BitArray(127);
BitArray bit128 = new BitArray(128);
BitArray bit129 = new BitArray(129);
BitArray bit255 = new BitArray(255);
BitArray bit256 = new BitArray(256);
BitArray bit257 = new BitArray(257);
Assert.assertEquals(1, bit10.getStorageArraySize());
Assert.assertEquals(2, bit127.getStorageArraySize());
Assert.assertEquals(2, bit128.getStorageArraySize());
Assert.assertEquals(3, bit129.getStorageArraySize());
Assert.assertEquals(4, bit255.getStorageArraySize());
Assert.assertEquals(4, bit256.getStorageArraySize());
Assert.assertEquals(5, bit257.getStorageArraySize());
}
@Test
public void testBitArrayInitValue(){
BitArray bit10 = new BitArray(10);
BitArray bit127 = new BitArray(127);
BitArray bit128 = new BitArray(128);
BitArray bit129 = new BitArray(129);
BitArray bit255 = new BitArray(255);
BitArray bit256 = new BitArray(256);
BitArray bit257 = new BitArray(257);
Assert.assertEquals(0l, bit10.getLongValueOfAllBit());
Assert.assertEquals(0l, bit127.getLongValueOfAllBit());
Assert.assertEquals(0l, bit128.getLongValueOfAllBit());
Assert.assertEquals(0l, bit129.getLongValueOfAllBit());
Assert.assertEquals(0l, bit255.getLongValueOfAllBit());
Assert.assertEquals(0l, bit256.getLongValueOfAllBit());
Assert.assertEquals(0l, bit257.getLongValueOfAllBit());
}
@Test
public void testBitArrayAllFalse(){
BitArray bit10 = new BitArray(10);
BitArray bit127 = new BitArray(127);
BitArray bit128 = new BitArray(128);
BitArray bit129 = new BitArray(129);
BitArray bit255 = new BitArray(255);
BitArray bit256 = new BitArray(256);
BitArray bit257 = new BitArray(257);
Assert.assertEquals(true, bit10.isAllFalse());
bit10.setBit(8, true);
Assert.assertEquals(false, bit10.isAllFalse());
bit10.setBit(8, false);
Assert.assertEquals(true, bit10.isAllFalse());
randomSetField(bit10);
randomSetField(bit127);
randomSetField(bit128);
randomSetField(bit129);
randomSetField(bit255);
randomSetField(bit256);
randomSetField(bit257);
}
public static void randomSetField(BitArray pBitArray){
int bitArraySize = pBitArray.getArraySize();
Random rand = new Random();
int settingIndex = rand.nextInt(bitArraySize);
Assert.assertEquals(true, pBitArray.isAllFalse());
pBitArray.setBit(settingIndex, true);
Assert.assertEquals(false, pBitArray.isAllFalse());
pBitArray.setBit(settingIndex, false);
Assert.assertEquals(true, pBitArray.isAllFalse());
int settingIndex1 = rand.nextInt(bitArraySize);
int settingIndex2 = rand.nextInt(bitArraySize);
int settingIndex3 = rand.nextInt(bitArraySize);
pBitArray.setBit(settingIndex1, true);
pBitArray.setBit(settingIndex2, true);
pBitArray.setBit(settingIndex3, true);
Assert.assertEquals(false, pBitArray.isAllFalse());
pBitArray.setBit(settingIndex1, false);
pBitArray.setBit(settingIndex2, false);
pBitArray.setBit(settingIndex3, false);
Assert.assertEquals(true, pBitArray.isAllFalse());
}
And thanks to below posts, actually the BitArray class comes from the last post.
http://stackoverflow.com/questions/15736626/java-how-to-create-and-manipulate-a-bit-array-with-length-of-10-million-bits
http://stackoverflow.com/questions/14145733/how-can-one-read-an-integer-bit-by-bit-in-java/14145767
http://stackoverflow.com/questions/4674006/set-specific-bit-in-byte
Comments