I inherited some code and can’t figure out one piece of it:
byte[] b = new byte[4] { 3, 2, 5, 7 };
int c = (b[0] & 0x7f) << 24 | b[1] << 16 | b[2] << 8 | b[3];
Can anyone tell what’s happening here?
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.
Basically it converts the lower 31 bits of a 4 byte array into an integer using a big-endian conversion.
So a byte array of { 0, 0, 0, 1 } would be converted to 1; a byte array of { 0, 0, 1, 0 } would be converted to 256 etc.
It does this through a mixture of bitwise operators:
&is bitwise “and”|is bitwise “or”<<is the left shift operator