shayster01
shayster01

Reputation: 214

JAVA Byte Manipulation

I want to read a binary file and do some manipulation on each byte. I want to test that I am manipulating the bytes correctly. I want to set a byte variable1 to "00000000" and then another byte variable2 set at "00001111" and OR them newvariable = variable1|variable2, shift the newvariable << 4 bits and then print out the int value.

 byte a = 00000000;
 //Convert first oneByte to 4 bits and then xor with a;
 byte b = 00001111;
 byte c = (byte)(a|b);
 c = c << 4;
 System.out.println("byte= " + c + "\n");

I am not sure why I keep getting "incompatiable types:possible lossy conversion from byte to int"

Upvotes: 0

Views: 2392

Answers (1)

Brick
Brick

Reputation: 4272

You need to put a '0b' in front of those numbers to express binary constants. The number 00001111 is interpreted as a literal in octal, which is 585 in decimal. The max byte is 127 (since it's signed). Try 0b00001111 instead.

As literals, those will still be int, so depending on where you do the assignment, you may also need to explicitly cast down to byte.

Upvotes: 3

Related Questions