left shift and right shift operators ??

Ingo Krabbe ikrabbe@earthling.net
Wed Jun 20 06:12:00 GMT 2001


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




More information about the Gcc-help mailing list