I currently use these two functions to pack and read bits in a byte array. Wondering if anybody has any better ideas or faster ways to do it?
Edited the program with a few more optimization and tabled a few calculations. Currently 100mil Put and Get takes about 12 secs instead of 16 secs now.
If anybody is using the current code make sure the value passed in to Put is a positive number as it's expecting unsigned numbers coming down. If there is interest I can put up signed and unsigned versions.
class BitData
{
static void Put(byte Data[], final int BitOffset, int NumBits, final int Value)
{
final long valLong=(Value&((1L<<NumBits)-1L));
int posByte=BitOffset>>3;
int posBit=BitOffset&7;
int valByte;
int ModifyBits;
long lValue;
int LeftShift;
ModifyBits=8-posBit;
if(NumBits<ModifyBits) ModifyBits=NumBits;
LeftShift=(8-posBit-ModifyBits);
while(true)
{
valByte = Data[posByte];
if(ModifyBits==8)
{
lValue=valLong<<(32-NumBits)>>(24);
Data[posByte]=(byte)lValue;
}
else
{
lValue=valLong<<(32-NumBits)>>(32-ModifyBits)<<LeftShift;
Data[posByte]=(byte)((valByte & ~(((1<<ModifyBits)-1) << LeftShift)) | lValue);
}
NumBits-=ModifyBits;
if(NumBits==0) break;
posByte++;
ModifyBits=8;
if(NumBits<ModifyBits)
{
ModifyBits=NumBits;
LeftShift=(8-ModifyBits);
}
}
}
static int GetInt(byte Data[], final int BitOffset, int NumBits)
{
int posByte=BitOffset>>3;
int posBit=BitOffset&7;
long Value=0;
int ModifyBits;
int valByte;
int LeftShift;
ModifyBits=8-posBit;
if(NumBits<ModifyBits) ModifyBits=NumBits;
LeftShift=(8-posBit-ModifyBits);
while(true)
{
valByte = Data[posByte] & 0xff;
if(ModifyBits==8) Value+=valByte;
else Value+=(valByte & ((1<<ModifyBits)-1) << LeftShift) >> LeftShift;
NumBits-=ModifyBits;
if(NumBits==0) break;
posByte++;
ModifyBits=8;
if(NumBits<ModifyBits)
{
ModifyBits=NumBits;
LeftShift=(8-ModifyBits);
}
Value<<=ModifyBits;
}
return (int)Value;
}
}
A totally different route would be to define static table of all possible combinations and perform a lookup instead of calculating results each time. I think thats how they do it in cryptography. array[i] x 3 should be much faster than numBits bitwise operations. It will occupy some heap though.
来源:https://stackoverflow.com/questions/7604653/what-is-the-most-efficient-way-in-java-to-pack-bits-into-byte-and-read-it-back