• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: gcc and bitwise right shift
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: gcc and bitwise right shift


  • Subject: Re: gcc and bitwise right shift
  • From: Jeremy Pereira <email@hidden>
  • Date: Wed, 18 Mar 2009 11:14:42 +0000


On 17 Mar 2009, at 19:09, Thierry Faucounau wrote:

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.

This is beside the main point, but your understanding is incorrect. The C compiler uses an arithmetic shift for right shifts which means the sign is preserved. e.g.


#include <stdio.h>

int main ()
{
    int foo = -2 ;
    unsigned int bar = -2 ;

    printf ("%d x\n", foo >> 1, foo >> 1) ;
    printf ("%u x\n", bar >> 1, bar >> 1) ;
    return 0;
}

when compiled and run gives the following:

-1 ffffffff
2147483647 7fffffff

(unsigned integers have an implicit sign of 0 and shifts are implemented as logical shifts).


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?

As has already been stated, for shift amounts greater than the number of bits, the result is undefined. If you modify your program to print the result of a different number right shifted, it immediately becomes obvious what gcc actually does:


8 >> 0 = 8
8 >> 1 = 4
8 >> 2 = 2
8 >> 3 = 1
8 >> 4 = 0

<snip>

8 >> 31 = 0
8 >> 32 = 8
8 >> 33 = 4
8 >> 34 = 2
8 >> 35 = 1
8 >> 36 = 0

<snip>

8 >> 63 = 0
8 >> 64 = 8
8 >> 65 = 4
8 >> 66 = 2
8 >> 67 = 1
8 >> 68 = 0

It's shifting by the number of bits specified modulo the integer width. This is because it just goes with the default behaviour of the Intel sar (shr for unsigned) instruction. You might find that the same code compiled for PPC gives different behaviour.



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

_______________________________________________ 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
  • Follow-Ups:
    • Re: gcc and bitwise right shift
      • From: Thierry Faucounau <email@hidden>
    • Re: gcc and bitwise right shift
      • From: John Engelhart <email@hidden>
References: 
 >gcc and bitwise right shift (From: Thierry Faucounau <email@hidden>)

  • Prev by Date: Re: Per architecture file subsets in a Compile files build phase
  • Next by Date: Re: xcodebuild slow to return?
  • Previous by thread: Re: gcc and bitwise right shift
  • Next by thread: Re: gcc and bitwise right shift
  • Index(es):
    • Date
    • Thread