gcc and bitwise right shift
gcc and bitwise right shift
- Subject: gcc and bitwise right shift
- From: Thierry Faucounau <email@hidden>
- Date: Tue, 17 Mar 2009 20:09:04 +0100
As far as I understood it, bitwise shifts using << or >> in C were
logical shifts (ie. bringing in zeros). However, I just hit a
difference in behavior between the windows compiler and gcc and I haev
to say, I sort of side with VC++ on this one.
It seems that for certain special amounts of right shift, you'll
actually get back the original number instead of the expected result
(in my case 0).
This is with:
gcc -v
Using built-in specs.
Target: i686-apple-darwin9
Configured with: /var/tmp/gcc/gcc-5488~2/src/configure --disable-
checking -enable-werror --prefix=/usr --mandir=/share/man --enable-
languages=c,objc,c++,obj-c++ --program-transform-name=/^[cg][^.-]*$/s/
$/-4.0/ --with-gxx-include-dir=/include/c++/4.0.0 --with-slibdir=/usr/
lib --build=i686-apple-darwin9 --with-arch=apple --with-tune=generic --
host=i686-apple-darwin9 --target=i686-apple-darwin9
Thread model: posix
gcc version 4.0.1 (Apple Inc. build 5488)
At first I did some poking around in gdb (since I was staring at the
code in question from the debugger):
(gdb) p (int)(1 >> 64)
$4 = 1
(gdb) p (int)(1 >> 63)
$5 = 0
(gdb) p (int)(1 >> 62)
$6 = 0
(gdb) p (int)(50 >> 64)
$8 = 50
(gdb) p (int)(50 >> 63)
$9 = 0
Not what I was expecting, so I wrote a small test:
#include <stdio.h>
int main (int argc, char * const argv[])
{
int i;
for (i = 0; i < 257; i++)
{
printf("1 >> %d = %d\n", i, (1 >> i));
}
return( 0 );
}
And ran that.
$ gcc test_shift.c
$ ./a.out | grep "= 1"
1 >> 0 = 1
1 >> 32 = 1
1 >> 64 = 1
1 >> 96 = 1
1 >> 128 = 1
1 >> 160 = 1
1 >> 192 = 1
1 >> 224 = 1
1 >> 256 = 1
How odd (apart from the first one ofc). What am I missing here?
Thierry
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden