Are (bool)(i & 1) and i % 2 == 1 always same where i is int?
Note: saying always I mean for all platforms (even when a byte is 16 bit) and for all standards of C and C++.
Edit:
For all standards of C and C++ where bool exist.
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.
No.
1s’ complement representation of
int, the representation of -1 is1 ... 10, so they differ.Anyway,
i % 2can be negative for negativei(indeed it’s required to be in C99 when it’s not 0), and hence not equal to 1 for negative odd numbers.