0

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"

1

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Adding '0b' infront of byte a and byte b fixes it there . Then settingbyte c = (byte)(c << 4); even if casting it by c = c (byte) << 4 works printing -16 as expected!
00001111 is 1111 in octal and 585 in decimal
@saka1029 Good catch. Updating my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.