Can someone explain the following piece of code
int x = 45;
int y = x &= 34;
It assigns 32 to y
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 performing a bitwise “and” as a compound assignment operator. It’s equivalent to:
Now 45 = 32 + 8 + 4 + 1, and 34 = 32 + 2, so the result of a bitwise “and” is 32.
Personally I think that using a compound assignment operator in a variable declaration is pretty unreadable – but presumably this wasn’t “real” code to start with…