This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: left shift and right shift operators ??
- To: Ravi <ravee_26 at yahoo dot com>
- Subject: Re: left shift and right shift operators ??
- From: Ingo Krabbe <ikrabbe at earthling dot net>
- Date: Wed, 20 Jun 2001 14:15:07 +0200 (CEST)
- cc: gcc-help <gcc-help at gcc dot gnu dot org>
- Reply-To: Ingo Krabbe <i dot krabbe at dokom dot net>
On Tue, 19 Jun 2001, Ravi wrote:
> Hi,
> I am new to the C language. I would like to know what is the practical
> use of left shift "<<" and right shift ">>" operators.
>
> Another doubt is how do I predict what i get when i write a code like say :
>
> int x = 25;
> printf("%d", x << 8 );
>
> What does the compiler do ? does it convert the integer 25 into a binary
> number and shift it left by 8 bits ? And what is the practical use of writing
> such a code ?
This operators are quite near to the machine. Any processors (I know) have
a binary shift operation. The integer is internally known as a binary
number to the processor. In fact an integer fills exactly one register of
your processor. This register is shifted left by << and right by >> the
many bits you want.
Just try to shift a bit:
for ( int i = 1; i != 0; i<<1 )
printf( "%x", i );
Look what it does and you will know what a shift is.
CU INGO