I am trying to make a test to tell whether my PC performs arithmetic or logical right shift by right-shifting hexadecimal FFFFFFFF
by 1
.
I know that an integer -1
reads as FFFFFFFF
in hexadecimal since it is the two's complement of 1
. Right-shifting -1
by 1
results in FFFFFFFF
and shows the PC performed arithmetic right shift.
But if I just type in 0xFFFFFFFF >> 1
, it resulted in 7FFFFFFF
and shows that the PC performed logical right shift instead. Why did that happen? See for the code below that produced the results:
#include <stdlib.h>
#include <stdio.h>
int main ( int argc, char *argv[] )
{
printf ( "%x >> 1 = %x\n", -1, -1 >> 1 );
printf ( "%x >> 1 = %x\n", 0xffffffff, 0xffffffff >> 1 );
return EXIT_SUCCESS;
}
The program's output was:
ffffffff >> 1 = ffffffff
ffffffff >> 1 = 7fffffff
-1
is signed.0xffffffff
is unsigned. It has nothing to do withprintf()
.-1 >> 1
could print as7fffffff
as well.