What does the ampersand mean in this code?
int clothes = (random.nextInt(0x1000000) & 0x7f7f7f);
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.
It’s the bitwise AND operator.
It operates on each bit position independently; the output bit in position n is only 1 if both the corresponding input bits in position n are also 1.
In this context, the
0x7f7f7fis being used as a bit-mask. By having certain bit positions as 0, it means that the corresponding bit positions inclotheswill always be 0. All other bit positions will take on the same value as produced byrandom.nextInt(0x1000000).