How can I prevent the gcc optimizer from producing incorrect bit operations?
问题 Consider the following program. #include <stdio.h> int negative(int A) { return (A & 0x80000000) != 0; } int divide(int A, int B) { printf("A = %d\n", A); printf("negative(A) = %d\n", negative(A)); if (negative(A)) { A = ~A + 1; printf("A = %d\n", A); printf("negative(A) = %d\n", negative(A)); } if (A < B) return 0; return 1; } int main(){ divide(-2147483648, -1); } When it is compiled without compiler optimizations, it produces expected results. gcc -Wall -Werror -g -o TestNegative