Le 14-06-2006,
to********@gmail.com <to********@gmail.com> a écrit*:
deepak wrote: If i'm shifting an integer 'n' times where n > sizeof(int), It's giving
the same value as
that of n-sizeof(n). Why is it behaving like this. I studied that the
bit 'll be replaced by '0'.
Example;
int i = 1;
printf ("%d\n", i<<32);
o/p: 1.
int i = 2;
printf ("%d\n", i<<33);
o/p: 2.
What compiler is giving you that output? I know GCC will happily
oblige you with the desired zeroes.
Sure ?
-------------------- shift.c -------------------------
#include <limits.h>
#include <assert.h>
#include <stdio.h>
int main() {
assert( sizeof(int) == 4 && CHAR_BIT == 8 );
unsigned int taille = 32;
unsigned int r1 = 1u << taille;
printf("** %u\n", r1);
unsigned int r2 = 1u << 32;
printf("** %u\n", r2);
return 0;
}
-------------------- shift.c -------------------------
Run on a sparc
pogo% uname -a
SunOS pogo 5.9 Generic_118558-06 sun4u sparc SUNW,Sun-Blade-1000
pogo% gcc -v
gcc version 3.4.5
pogo% gcc shift.c ; ./a.out
shift.c: In function `main':
shift.c:10: warning: left shift count >= width of type
** 1
** 1
Run on intel
ubu> uname -a
Linux ubu 2.6.12-10-386 #1 Mon Feb 13 12:13:15 UTC 2006 i686 GNU/Linux
ubu> gcc -v
version gcc 4.0.2 20050808 (prerelease) (Ubuntu 4.0.1-4ubuntu9)
ubu> gcc shift.c ; ./a.out
shift.c: In function 'main':
shift.c:10: warning: left shift count >= width of type
** 1
** 0
Marc Boyer