What is the best way to do java encoding for these types of byte level operations?
I am reading about some of the problems with optimization approaches.
In the problem of how to sort numbers in a specific range, the solution is to use a bitmap. And if a number can appear, for example. use half bytes up to 10 times to match numbers and as counters to represent the number of occurrences.
A concept that I understand well. My problem is how to implement this in Java in a simple manner.
I am stuck with bit operations.
For example, for the first part, to increment the counter by 1, I might think:
Find byte
For example. bitValue[i]
Then do byte tmp = bitValue[i] & 0x0F
to get the least significant bits (if the counter is a low frequency counter).
Then do tmp = tmp + 1
to increase by 1.
Then do bitValue[i] >> 2
to clear the least significant bits and then bitValue[i] <<2
to restore. We now have the same high bits as the original, and the low bit is transparent.
Then do bitValue[i] |= tmp
to set the beats low.
Now bitValue
has a low bit counter incremented by 1. Right?
For the top bit, it will be the same process but for the top bits.
Then when I need to check what the counter number is.
I was thinking to use bitmasks: 0x0
0x1
0x2
etc. and use OR
to check what the current counter number is.
It all seems too complicated. Am I on the right track? How are these operations best suited for java encoding?
Any input, guidance on this is appreciated.
source to share
You are definitely on the right track. Here's some flashing code that increments the first four bits or four seconds int
by a specified amount.
Note that instead byte
is used here int
. Even if your data is byte
, it's usually easier to work with int
. This is because java is bitwise operators like |
and &
and <<
return int
. So its easiest to work with your data as int
and then discard once you've done all your bit squeaks.
Also, if you need to deal with a lot of data (possibly more than the two counters you specify) at the bit level, you might want to consider BitSet .
public class Test {
public static void main(String[] args)
{
int counter = 0;
// increment the low bits by 3 and high bits by 2
counter = addLowBits( counter, 3 );
counter = addHighBits( counter, 2 );
// print the hex string to verify
System.out.println( Integer.toHexString( counter ) );
System.out.println( "Low Counter: " + ( counter & 0x0F ) );
System.out.println( "High Counter: " + ( ( counter & 0xF0 ) >> 4 ) );
}
public static int addLowBits( int counter, int increment )
{
// get the low bits
int low = counter & 0x0F;
// increment by 1
low = low + increment;
// mask the high bits and insert new low bits
counter = (counter & 0xF0) | low;
return counter;
}
public static int addHighBits( int counter, int increment )
{
// now get high bits
int high = ( counter & 0xF0 ) >> 4;
// increment by 1
high = high + increment;
// mask the low bits and insert new high bits
counter = (counter & 0x0F) | ( high << 4 );
return counter;
}
}
source to share