How to assemble bytes from a bit stream? - java

I'm trying to make bytes out of a source of individual bits /booleans in the Java language. I'd like the routine to run as quickly as possible, and I'm after the unsigned flavour of byte. I've done some internet searches, but can't actually find any code specifically for this.
All I can come up with is the following, which seems amateurish. I'm aware of the BitSet, but those are internally represented as Longs (I'm using a 32 bit machine so double handling) and it has a huge API so I expect it to be slow. Can there be anything more efficient, perhaps at low level?
public int nextByte() {
int b = 0;
for (int k = 0; k < 7; k++) {
if (nextBoolean()) {
b++;
}
b = b << 1;
}
if (nextBoolean()) {
b++;
}
return b;
}

This should be as efficient as it can be:
public int nextByte() {
int b = nextBoolean() ? 0x80 : 0;
if (nextBoolean()) b = b | 0x40;
if (nextBoolean()) b = b | 0x20;
if (nextBoolean()) b = b | 0x10;
if (nextBoolean()) b = b | 0x08;
if (nextBoolean()) b = b | 0x04;
if (nextBoolean()) b = b | 0x02;
if (nextBoolean()) b = b | 0x01;
return b;
}
If you wish to try really, really hard, you may use:
public int nextByte() {
return
(nextBoolean() ? 0x80 : 0)
| (nextBoolean() ? 0x40 : 0)
| (nextBoolean() ? 0x20 : 0)
| (nextBoolean() ? 0x10 : 0)
| (nextBoolean() ? 0x08 : 0)
| (nextBoolean() ? 0x04 : 0)
| (nextBoolean() ? 0x02 : 0)
| (nextBoolean() ? 0x01 : 0);
}
If this were C language, this last solution would not be guaranteed to work, as the C standard says calls to nextBoolean() may occur in any order the compiler sees fit. However, the Java Language Specification, section 15.7, guarantees (apparent) strict left-to-right evaluation, so in Java bits are guaranteed to go in the correct order. On the other hand, the next sentence explicitly recommends code to "not rely crucially on this specification" and that "code is usually clearer when each expression contains at most one side effect". Of course, calling nextBoolean() is a textbook example of "a side effect", and doing it 8 times in a single expression goes against the recommendation.
Furthermore (and most importantly, and in line with other answers), I say the most computationally expensive part of these solutions (or yours, for that matter) is probably the eight calls to nextBoolean() and not the loop or the conditionals, so I don't think you can get this going visibly faster without avoiding those calls.

Related

How do I use bitwise operators to accomplish this?

int a = 0b1011011011;
int b = 0b1000110110;
int c = 0b0101010101;
int d = (a & b) ^ c; //Wrong
Intended value ofd is 0b1010011110
I need to write d so that when the bit of c is 1, the corresponding bit in the result is the corresponding bit in b, but when the bit of c is 0, the corresponding bit in the result is the corresponding bit in a.
I've been stuck on this for awhile now and I can't seem to come up with something in one line.
I had this earlier but didn't see your edit.
int d = (c & b)^(~c & a) ;
q = c & b yields b when c is 1 regardless of b.
p = ~c & a yields a when c is 0 regardless of a.
q ^ p simply preserves those bits exclusive to a or b and 0 otherwise.
I get the feeling that this is for homework, but I'll answer anyway, since it's hard to explain this without giving away the answer.
Consider two simpler questions. Forget about the multiple bits and pretend a, b, c, and d are only one bit (since this is a bitwise operation, the logic will not change):
When c is 1, d = b. When c is 0, d = 0.
When c and b are both 1, d ends up being 1. If b or c is 0, d is 0.
This means that d = b & c.
When c is 0, d = a. When c is 1, d = 0.
This is very similar to case #1, except c is flipped and a is replaced with b.
Therefore, we can replace b with a, and c with ~c to get this solution.
This means that d = a & ~c.
Now for your original question: if we take those two simpler examples, we can see that it is impossible for both of them to be 1. So if we want both rules to apply, we can just put an | between them, giving us:
d = (b & c) | (a & ~c).
I need to write d so that when the bit of c is 1, the corresponding bit in the result is the corresponding bit in b,
when c == 1 d = b
but when the bit of c is 0, the corresponding bit in the result is the corresponding bit in a.
when c == 0 d = a
This sounds like a job for bit masking!
I know you give these as your test data:
int a = 0b1011011011;
int b = 0b1000110110;
int c = 0b0101010101;
int d = 0b1010011110;
But this test data is just as good a test and easier to read. All I've done is rearrange the bit columns so that c doesn't change so often:
int a = 0b11011_01101;
int b = 0b10101_00110;
int c = 0b00000_11111;
int d = 0b11011_00110;
Since Java 7 we can also use underscores in numeric literals to make them a little easier on the eyes.
It should now be easy to see that c controls where d is copied from, a or b. Easy to read data is important too.
And now, some bit masks
assertTrue(b & c ==0b00000_00110);
assertTrue(a & ^c==0b11011_00000);
Or them together and you get:
int d = 0b11011_00110;
assertTrue(d == (b & c) | (a & ^c));
Try that with either data set. Should still work.
You could xor them together as well. Doesn't matter because the c mask negation already excludes the potential for 1's on both sides. I chose or simply out of a sense of tradition.

What are the bit mask operations that I can perform?

I am using java SWT, which has a bunch of bit flags and operations I can use, but I'm not familiar with them.
To better explain, I have a style
style = SWT.A | SWT.B
Which basically translates to having style A AND B. I know that this is because
A = 0001
B = 0100
A | B = 0101 (bitwise or)
But I haven't played with bits enough to know all the things I can do, this is all I know
style |= A; // style adds flag A
style &= ~B; // style removes flag B
Do I have something like +0 at my disposal? For ternary operations.
style ?= question ? "+ style A" : "as is, no change"
I'm thinking maybe
style = question ? style | A : style;
style = question ? style & ~B : style;
But I'm not sure.
Anything else that would be useful?
There is also exclusive OR.
Exclusive OR (aka XOR) says in shorthand, one or the other but not both. So if you XOR 0 and 1 together it will return a 1. Otherwise a 0. And don't forget that these bitwise operators also operate on boolean values.
int A = 0b0001;
int B = 0b0100;
// A | B = 0101 (bitwise or)
style ^= A; // If off, turn on. If on, turn off.
style = A|B; // 0101
style ^= A; // style now equals 0100
style ^= A; // style now equals 0101
You can also swap with it.
int a = 23;
int b = 47;
a ^= b;
b ^= a;
a ^= b;
Now a == 47 and b == 23
And lastly, there is another use for bitwise operators. Defeating short circuiting of if statements. Here is an example:
int a = 5;
int b = 8;
// here a is true, no need to evaluate second part, it is short circuited.
if (a == 5 || ++b == 7) {
System.out.println(a + " " + b);
}
// but here the second part is evaluated and b is incremented.
if (a == 5 | ++b == 7) {
System.out.println(a + " " + b);
}
I can't remember every using it this way and it can cause difficult to find bugs in your program. But it's a feature.

How does the code work - "SWT.MULTI | SWT.SINGLE" when applied to style

This is a code i am seeing for some time.
i wonder how the code works.
public static final int MULTI = 1 << 1;
public static final int SINGLE = 1 << 2;
public static final int READ_ONLY = 1 << 3;
SWT.MULTI | SWT.SINGLE | SWT.READ_ONLY
i went deep into the class implementation of TableViewer(Composite parent, int style) in search for the answer but didnt find much.
I found this code, but didnt understand much
static int checkBits (int style, int int0, int int1, int int2, int int3, int int4, int int5) {
int mask = int0 | int1 | int2 | int3 | int4 | int5;
if ((style & mask) == 0) style |= int0;
if ((style & int0) != 0) style = (style & ~mask) | int0;
if ((style & int1) != 0) style = (style & ~mask) | int1;
if ((style & int2) != 0) style = (style & ~mask) | int2;
if ((style & int3) != 0) style = (style & ~mask) | int3;
if ((style & int4) != 0) style = (style & ~mask) | int4;
if ((style & int5) != 0) style = (style & ~mask) | int5;
return style;
}
Packing Bits Together
Let's take a look at this code.
public static final int MULTI = 1 << 1;
public static final int SINGLE = 1 << 2;
public static final int READ_ONLY = 1 << 3;
The "<<" operator means shift the bits to the left. The number after the "<<" operator tells us how many bits to shift.
So, another way of writing the code would be.
public static final int MULTI = 2;
public static final int SINGLE = 4;
public static final int READ_ONLY = 8;
By defining the flags this way, the values can be OR'd ("added") together and saved in one byte or one integer.
SWT.MULTI | SWT.SINGLE
This is saying to OR the bits of the two values together. But what does that mean?
In this case, since we've defined our values as a single bit, it's the same as adding the values.
MULTI = 2
SINGLE = 4
Therefore, the status value is 6 (2 + 4).
Now remember, we're not adding. Because we're using bits, the effect is the same as if we're adding.
Unpacking Bits
The second bit of code you've posted basically takes the 6 that we came up with, and splits it back into 2 and 4. It works by checking each bit, one at a time, and seeing if it's present.
Let's take one line, and see what it's doing.
if ((style & int1) != 0) style = (style & ~mask) | int1;
int1 represents one of the style bits. For the sake of this discussion, let's say that it is MULTI with a value of 2.
The first part (the if condition) tests for whether or not the bit is set in the style integer.
The second part is a bit trickier. (Bit, get it. Very punny.)
The value of mask is given in the code.
int mask = int0 | int1 | int2 | int3 | int4 | int5;
This is just all of the status values OR'd together. From our original example of MULTI, SINGLE, and READ_ONLY, that gives us a mask of x'0E' or 14. Through the rest of this discussion, I'll be using hexadecimal.
Status Bit Present
Now, back to the line we're talking about.
if ((style & int1) != 0) style = (style & ~mask) | int1;
style & int1 is an AND operation. That means the bit has to be set in both style and int1. Style is x'06' (2 + 4) from when we set it before. int1 is x'02'. When you AND x'06' and x'02', you get x'02', which makes the value of the if true.
~mask converts the x'0E' into x'F1'. In other words, all of the bits are flipped from 0 to 1 and 1 to 0.
style & ~mask is an AND operation. When you AND x'06' and x'F1', you get x'00'. In other words, none of the bits match up.
We said earlier that int1 is MULTI, with the value of 2 or x'02'. Now we do an OR operation, which we explained before. ORing x'00' and x'02' gives us x'02', which is the MULTI value that we wanted to extract.
Status Bit Not Present
The other alternative (when the status bit is not present) leads to a slightly different result. We didn't set READ_ONLY, so lets go through that calculation.
READ_ONLY is x'08'. Style is x'06' When you AND those values together in the if
if ((style & int1) != 0) style = (style & ~mask) | int1;
you get x'00', which causes the value of the if to be false.
Justification
The original reason for putting several status values into one byte or word was to save memory. This was important 40 years ago when computer memory was limited. Now, it's done for convenience in passing status around. Rather than passing 7 or 15 different status indicators from one method to another, you pass one status indicator.
The tradeoff, as you have seen, is that it takes a bit of code to extract the status bits so you can use them.

Change bits value in Byte

I have some data in field type Byte ( I save eight inputs in Byte, every bit is one input ).
How to change just one input in that field ( Byte) but not to lose information about others ( example change seventh bit to one, or change sixth bit to zero )?
To set the seventh bit to 1:
b = (byte) (b | (1 << 6));
To set the sixth bit to zero:
b = (byte) (b & ~(1 << 5));
(The bit positions are effectively 0-based, so that's why the "seventh bit" maps to 1 << 6 instead of 1 << 7.)
Declare b as the primitive type byte:
byte b = ...;
Then you can use the compound assignment operators that combine binary operations and assignment (this doesn't work on Byte):
b |= (1 << bitIndex); // set a bit to 1
b &= ~(1 << bitIndex); // set a bit to 0
Without the assignment operator you would need a cast, because the result of the | and & operations is an int:
b = (byte) (b | (1 << bitIndex));
b = (byte) (b & ~(1 << bitIndex));
The cast is implicit in the compound assignment operators, see the Java Language Specification.
To set a bit use :
public final static byte setBit(byte _byte,int bitPosition,boolean bitValue)
{
if (bitValue)
return (byte) (_byte | (1 << bitPosition));
return (byte) (_byte & ~(1 << bitPosition));
}
To get a bit value use :
public final static Boolean getBit(byte _byte, int bitPosition)
{
return (_byte & (1 << bitPosition)) != 0;
}
Note that the "Byte" wrapper class is immutable, and you will need to work with "byte".
You really owe it to yourself to look into masking functions for and, or, and xor -- they allow you to simultaneously verify, validate, or change... one, some, or all of the bits in a byte structure in a single statement.
I'm not a java programmer by trade, but it's derived from C and a quick search online seemed to reveal support for those bitwise operations.
See this Wikipedia article for more information about this technique.

Converting Little Endian to Big Endian

All,
I have been practicing coding problems online. Currently I am working on a problem statement Problems where we need to convert Big Endian <-> little endian. But I am not able to jot down the steps considering the example given as:
123456789 converts to 365779719
The logic I am considering is :
1 > Get the integer value (Since I am on Windows x86, the input is Little endian)
2 > Generate the hex representation of the same.
3 > Reverse the representation and generate the big endian integer value
But I am obviously missing something here.
Can anyone please guide me. I am coding in Java 1.5
Since a great part of writing software is about reusing existing solutions, the first thing should always be a look into the documentation for your language/library.
reverse = Integer.reverseBytes(x);
I don't know how efficient this function is, but for toggling lots of numbers, a ByteBuffer should offer decent performance.
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
...
int[] myArray = aFountOfIntegers();
ByteBuffer buffer = ByteBuffer.allocate(myArray.length*Integer.BYTES);
buffer.order(ByteOrder.LITTLE_ENDIAN);
for (int x:myArray) buffer.putInt(x);
buffer.order(ByteOrder.BIG_ENDIAN);
buffer.rewind();
int i=0;
for (int x:myArray) myArray[i++] = buffer.getInt(x);
As eversor pointed out in the comments, ByteBuffer.putInt() is an optional method, and may not be available on all Java implementations.
The DIY Approach
Stacker's answer is pretty neat, but it is possible to improve upon it.
reversed = (i&0xff)<<24 | (i&0xff00)<<8 | (i&0xff0000)>>8 | (i>>24)&0xff;
We can get rid of the parentheses by adapting the bitmasks. E. g., (a & 0xFF)<<8 is equivalent to a<<8 & 0xFF00. The rightmost parentheses were not necessary anyway.
reversed = i<<24 & 0xff000000 | i<<8 & 0xff0000 | i>>8 & 0xff00 | i>>24 & 0xff;
Since the left shift shifts in zero bits, the first mask is redundant. We can get rid of the rightmost mask by using the logical shift operator, which shifts in only zero bits.
reversed = i<<24 | i>>8 & 0xff00 | i<<8 & 0xff0000 | i>>>24;
Operator precedence here, the gritty details on shift operators are in the Java Language Specification
Check this out
int little2big(int i) {
return (i&0xff)<<24 | (i&0xff00)<<8 | (i&0xff0000)>>8 | (i>>24)&0xff;
}
The thing you need to realize is that endian swaps deal with the bytes that represent the integer. So the 4 byte number 27 looks like 0x0000001B. To convert that number, it needs to go to 0x1B000000... With your example, the hex representation of 123456789 is 0x075BCD15 which needs to go to 0x15CD5B07 or in decimal form 365779719.
The function Stacker posted is moving those bytes around by bit shifting them; more specifically, the statement i&0xff takes the lowest byte from i, the << 24 then moves it up 24 bits, so from positions 1-8 to 25-32. So on through each part of the expression.
For example code, take a look at this utility.
Java primitive wrapper classes support byte reversing since 1.5 using reverseBytes method.
Short.reverseBytes(short i)
Integer.reverseBytes(int i)
Long.reverseBytes(long i)
Just a contribution for those who are looking for this answer in 2018.
I think this can also help:
int littleToBig(int i)
{
int b0,b1,b2,b3;
b0 = (i&0x000000ff)>>0;
b1 = (i&0x0000ff00)>>8;
b2 = (i&0x00ff0000)>>16;
b3 = (i&0xff000000)>>24;
return ((b0<<24)|(b1<<16)|(b2<<8)|(b3<<0));
}
Just use the static function (reverseBytes(int i)) in java which is under Integer Wrapper class
Integer i=Integer.reverseBytes(123456789);
System.out.println(i);
output:
365779719
the following method reverses the order of bits in a byte value:
public static byte reverseBitOrder(byte b) {
int converted = 0x00;
converted ^= (b & 0b1000_0000) >> 7;
converted ^= (b & 0b0100_0000) >> 5;
converted ^= (b & 0b0010_0000) >> 3;
converted ^= (b & 0b0001_0000) >> 1;
converted ^= (b & 0b0000_1000) << 1;
converted ^= (b & 0b0000_0100) << 3;
converted ^= (b & 0b0000_0010) << 5;
converted ^= (b & 0b0000_0001) << 7;
return (byte) (converted & 0xFF);
}

Categories