for (unsigned int i = 1; i <= 100; i++) {
if (i & 0x00000001) {
std::cout << i<<",";
}
}
why does (and how): if( i & 0x00000001 ) figure out the odd number?
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.
0x00000001is1in binary, although it’s written in hexadecimal (base-16) notation. That’s the0xpart.&is the bit-wise ‘AND’ operator, which is used to do binary digit (bit) manipulations.i & 1converts all of the binary digits of i to zero, except for the last one.It’s straightforward to convert the resulting 1-bit number to a boolean, for evaluation by the
ifstatement.The following chart shows the last 16 binary digits of i, and what happens to them.