Variable intVar is primitive int, bb[] is byte array {0x02,0xF7,0x8B,0xF9};
intVar = bb[0];
//intVar = 00000002
intVar <<= 8;
//intVar = 00000200
intVar |= bb[1];
//intvar = fffffff7
Why????
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The reason is sign extension. Use:
Using a negative byte (java bytes are signed) as int results in a negative int, and because java uses two’s complement all leading bits will be 1. Anding that with 0xFF will get you the byte you had in the lowest 8 bits and 24 leading 0s.