
Originally Posted by
A34Chris
Yes its dropping it down by a factor of two but what about the on bits that are shifted in? I'm confused.
Just try this
Code:
#include <stdio.h>
void print_bits(int value ) {
unsigned int mask = 1 << 31;
int i;
printf("\n%12d = ", value );
for( i = 31; i >= 0; --i ) {
printf("%c", value & mask ? '1' : '0' );
mask >>= 1;
}
}
int main() {
int v = 123;
print_bits( v );
print_bits( v >> 2);
puts("");
v = -123;
print_bits( v );
print_bits( v >> 2);
puts("");
return 0;
}
my output
Code:
123 = 00000000000000000000000001111011
30 = 00000000000000000000000000011110
-123 = 11111111111111111111111110000101
-31 = 11111111111111111111111111100001
The only thing that might be a little confusing is that shifting -123 two positions to the right gives -31. But that is how integer truncation is defined. it gives the next lower value.
Kurt