Q:

Bit Shifting a Byte

I have the following code:
byte my_byte;   
my_byte |= (1 << 7);
my_byte |= (1 << 6);    

I am trying to set the 8th and 7th bit in my_byte to 1. Using the debugger I found that after executing the 1st line, the value of my_byte is -128. After executing the 2nd line, the value of my_byte is -64.
I thought the final result would have been 192 (2^7 + 2^6). Would anyone know what I am doing wrong?
Also, should I be initializing my_byte at the start? If so, how do I just set all the bits to 0?
Thank you for your help.

A:

The problem is that byte is signed, so 10000000 is translated (2's-complement) to -128. Use short instead of byte.
To init all the bits to 0 just do short my_byte = 0;
P.s : short is 16-bit unlike byte which is 8 bit, AFAIK there is no 8-bit unsigned primitive type in Java

