Mailinglist Archive: opensuse-programming (41 mails)

< Previous Next >
Re: [suse-programming-e] Strange behaviour of >> ?
  • From: Jerry Feldman <gaf@xxxxxxx>
  • Date: Wed, 20 Jul 2005 10:29:44 -0400
  • Message-id: <200507201029.44994.gaf@xxxxxxx>
On Wednesday 20 July 2005 5:57 am, Verdi March wrote:
> Hi,
> the following is compiled in 9.3, glibc-2.3.4-23.4, and
> gcc-3.3.5-5. The result is:
> mask=0 mask1=4294967295
>
> I would expect mask = mask1 = 0?
>
> Regards,
> Verdi
>
> ===========================================================
> #include <stdio.h>
>
> int main() {
> unsigned int mask = (unsigned int) -1 >> 32;
>
> int i = 0;
> unsigned int mask1 = ((unsigned int) -1) >> (32 - i);
>
> printf("mask=%u mask1=%u\n", mask, mask1);
> return 0;
> }
> ===========================================================
Marcus' comment that this is an undefined behavior is absolutely correct
(ISO C90 6.5.7), so if your code relies on mask1 (or mask) resulting in 0 is
probably a bad assumption and the code should be corrected.

Apparently what is happening in the second case is that ((unsigned int) -1)
is being changed to a signed int, and the sign bit is getting propagated.
BTW: On a 64-bit system (Itanium) the values are both 0.

--
Jerry Feldman <gaf@xxxxxxx>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9

< Previous Next >
Follow Ups
References