Why the output between main()
and func()
are different?
I want to get the sign bit with bitwise operation and '+'operation,
so I use this statement: (x >> 31) & 0x1
to determine whether the sign bit is 1
, and its worked well in the past, but today I meet this problem, why the output are different?
#include <stdio.h>
int func(int x)
{
printf("func:\n");
int minint = 0x1 << 31;
int addx = x + minint;
printf("x = %.8x\n", x);
printf("x + minint = %.8x\n",addx);
printf("right shift: %.8x\n",addx >> 31);
}
output:
func:
x = 80000000
x + minint = 00000000
right shift: ffffffff
int main()
{
printf("main:\n");
int x = 0x80000000;
int minint = 0x1 << 31;
int addx = x + minint;
printf("x = %.8x\n", x);
printf("x + minint = %.8x\n",addx);
printf("right shift: %.8x\n",addx >> 31);
func(0x80000000);
return 0;
}
output:
main:
x = 80000000
x + minint = 00000000
right shift: 00000000