tags:

views:

507

answers:

7

How can I assign 2 bytes to a variable in Java? I know I can do this:

byte val = 2; // this is one byte with 0000 0010

But I need to assign 2 bytes to val. How can I do this?

+3  A: 

Are you looking for a byte array of length 2? In this case:

byte[] val = new byte[2];
val[0] = 2;
val[1] = something else;
David M
You were 5 seconds faster *g*
Lucero
And I typed more... ;)
David M
Whoever downvoted this please at least tell me why...!
David M
-1 - He wants to store 2 bytes in one value. I'm assuming (s)he is looking for some bit combining. (Nothing personal)
jjnguy
See the above answer. I just downvoted you to give the right answer a better chance of rising to the top. I will un downvote you, don't worry. Your answer isn't 'wrong' just not right.
jjnguy
@jjnguy - To be fair, I asked if my answer was what he was looking for at the top of it - we've both made assumptions.
David M
Yup. But, looking at how he represents the byte at the bit level makes my 90% sure that he wants bit operations.
jjnguy
Yes, agree you may well be right with your interpretation.
David M
Thanks for re-up by the way.
David M
`:)` .
jjnguy
+1  A: 
byte[] bytes = new byte[2];
bytes[0] = 2;
bytes[0] = 26;
Lucero
+1  A: 

Either use an array of bytes, a (short) integer, or a BitSet.

streetpc
A: 

Are you talking about assigning 2 bytes or 2 bits?

byte b = 5; //0000 0101

You may also want to try http://java.sun.com/j2se/1.4.2/docs/api/java/util/BitSet.html

+7  A: 

As well as using an array of two bytes, you can use a short, which is guaranteed by the Java language spec to be 16 bits wide.

short x = 0x1234s; // assigns 0x34 to the lower byte, 0x12 to the higher byte.

If you have two bytes that you want to combine into a short, you'll need shift the higher byte by 8 bits and combine them with bitwise or:

byte b1 = 0x12;
byte b2 = 0x34;
short x = ((short)b1 << 8) | b2;

If you want to assign different bits to a single byte variable, then you do that with the right-shift and bitwise or operators as well. Bit n is identified by (1<<n). 0 is the first bit of the byte, 7 the last. So setting two bits is done like:

byte b = (1<<3)|(1<<2); // b is set to 0000 1100
Nat
I think you've screwed up your sign extension. I don't know if you got the operator precedence right.
Tom Hawtin - tackline
Yeah, I think you wanted (((short)b1)<<8) | b2.
Andrew Coleson
I've corrected the answer.
Nat
Sign extension only applies to right shifts. Do you mean type promotion? I've corrected the parentheses so that the bytes are promoted to shorts before being shifted. Doing it afterwards shifts the bit off the high end, leaving the high byte as zero.
Nat
+2  A: 

You can store two values in one field using XOR :)

byte a, b, c;
a = 5;
b = 16;

c = a ^ b;

...

byte temp_a, temp_b;
temp_a = c ^ 16; /* temp_a == 5 */
temp_b = c ^ 5;  /* temp_b == 16 */

As you might have noticed, you need one of the original values to retrieve the other, so this technique is not as ... useful as the bit-shift method suggested.

Christoffer
For shame! Giving him a loaded gun! But seriously, have you ever used this in real life?
Erich Mirabal
I've used this to save memory implementing a doubly-linked list in an embedded system. You can save the back and forward pointers in a single word. To traverse in either direction, as long as you have the pointer to the node you came from, you can find the next in the traversal order by xor-ing it with the two xor-ed pointers.
Nat
Nice. That makes perfect sense for embedded systems. +1 to you (if I could).
Erich Mirabal
That's pretty cool.
cdmckay
@Eric - I've used this in a commercial product, yes.
Christoffer
A: 

You might want to take a look at Javolution's Struct class. http://javolution.org/target/site/apidocs/javolution/io/Struct.html

It lets you lay out structures with fixed length members and specific memory alignments. The implementation is backed by a ByteBuffer, so you can allocate it using direct memory if you are interacting with native code.

gibbss