wrong result from left shift in java -


i have made variable in java, byte = 0xa6; //10100110 made :

system.out.println(integer.tohexstring( ((short)a<<8)&0xffff )); 

the result 0xa600. right result. when tried

system.out.println(integer.tohexstring( ((short)a<<3)&0xffff )); 

the expected result should : 0x530 (10100110000) got 0xfd30(1111110100110000) emm... can explain how got wrong result...??

thanks... :-)

the byte value a6 represents negative number (bytes signed in java). when cast short gets sign extended ffa6. shift operation executed integer values again sign extended ffffffa6. shift left 3 bits gives fffffd30 , taking lower 16 bits gives 0000fd30.

this not matter if shift 8 bits because shift out , mask additional 1 bits.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -