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.
No.
1s' complement representation of int
, the representation of -1 is 1 ... 10
, so they differ.
Anyway, i % 2
can be negative for negative i
(indeed it's required to be in C99 when it's not 0), and hence not equal to 1 for negative odd numbers.
No.
For example, try it if i is -1. -1 % 2 == -1
, and (bool) (-1 & 1)
is 1.
(Assuming 2-complement)
精彩评论