I was studying an open source code where I came across the following line
stringBytes[i] = (byte) (stringChars[i] & 0x00FF);
Can someone explain what is actually happening in this line ???
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.
Char consists of 2 bytes in Java and of course byte is single byte.
So in this operation:
A char value (16 bits) is being binary ANDED with number 0x00FF (binary: 0000 0000 1111 1111) to make it one byte.
By binary ANDING with
8 0s and 8 1syou’re basically masking off 8 left most OR most significant bits (MSB) of the char value thus leaving only 8 right most or least significant bits (LSB) intact. Then code is assigning resulting values to a byte by using cast(byte), which is otherwise anintvalue.