I wrote the following testcase: #include <iostream>
using namespace std; inline bool sign(double x){return !((*(((unsigned int*)&(x)) + 1))&0x80000000);} inline bool sign1(double x){return (x>=0.0);} inline bool cmp(double x,double y){ return ((*(((unsigned int*)&(x)) + 1))&0x80000000)^((*(((unsigned int*)&(y)) + 1))&0x80000000); } inline bool cmp1(double x,double y){ return ((x>=0.0)!=(y>=0.0)); } int main(void){ double a(1.0),b(1.0); cout << sign(a) << " " << sign(b) << " " << sign1(a) << " " << sign1(b) << endl; cout << cmp(a,b) << " " << cmp1(a,b) << endl; cout << a << " " << b << endl; } Its output is: g++-3.4 test.cpp -O3 && ./a.out 0 1 1 1 1 0 1 1 g++ test.cpp -O3 && ./a.out 1 1 1 1 1 0 1 1 g++-3.4 -v Reading specs from /usr/local/lib/gcc/i686-pc-linux-gnu/3.4.3/specs Configured with: ./configure --prefix=/usr/local --program-suffix=-3.4 --enable-threads=posix --with-cpu=i686 --with-arch=i686 --disable-nls Thread model: posix gcc version 3.4.3 g++ -v Reading specs from /usr/lib/gcc-lib/i586-suse-linux/3.3.2/specs Configured with: ../configure --enable-threads=posix --prefix=/usr --with-local-prefix=/usr/local --infodir=/usr/share/info --mandir=/usr/share/man --libdir=/usr/lib --enable-languages=c,c++,f77,objc,java,ada --disable-checking --enable-libgcj --with-gxx-include-dir=/usr/include/g++--with-slibdir=/lib --with-system-zlib --enable-shared --enable-__cxa_atexit i586-suse-linux Thread model: posix gcc version 3.3.2 20031216 (prerelease) (SuSE Linux) But the right output is: 1 1 1 1 0 0 1 1 It can be reached at any optimization level by using flag -fno-schedule-insns2. -- Summary: Bitwise operations work wrong with -O2 or -O3 optimization Product: gcc Version: 3.4.3 Status: UNCONFIRMED Severity: normal Priority: P2 Component: c++ AssignedTo: unassigned at gcc dot gnu dot org ReportedBy: evgeny at nowecom dot ru CC: gcc-bugs at gcc dot gnu dot org GCC build triplet: i686-pc-linux-gnu GCC host triplet: i686-pc-linux-gnu GCC target triplet: i686-pc-linux-gnu http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20729