Using Shift Operators in C

By: Fazal Emailed: 1727 times Printed: 2328 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Two shift operators shift the bits in an integer variable by a specified number of positions. The << operator shifts bits to the left, and the >> operator shifts bits to the right. The syntax for these binary operators is

x << n

and

x >> n

Each operator shifts the bits in x by n positions in the specified direction. For a right shift, zeros are placed in the n high-order bits of the variable; for a left shift, zeros are placed in the n low-order bits of the variable. Here are a few examples:

Binary 00001100 (decimal 12) right-shifted by 2 evaluates to binary 00000011 (decimal 3).

Binary 00001100 (decimal 12) left-shifted by 3 evaluates to binary 01100000 (decimal 96).

Binary 00001100 (decimal 12) right-shifted by 3 evaluates to binary 00000001 (decimal 1).

Binary 00110000 (decimal 48) left-shifted by 3 evaluates to binary 10000000 (decimal 128).

Under certain circumstances, the shift operators can be used to multiply and divide an integer variable by a power of 2. Left-shifting an integer by n places has the same effect as multiplying it by 2n, and right-shifting an integer has the same effect as dividing it by 2n. The results of a left-shift multiplication are accurate only if there is no overflow--that is, if no bits are "lost" by being shifted out of the high-order positions. A right-shift division is an integer division, in which any fractional part of the result is lost. For example, if you right-shift the value 5 (binary 00000101) by one place, intending to divide by 2, the result is 2 (binary 00000010) instead of the correct 2.5, because the fractional part (the .5) is lost. Sample program demonstrates the shift operators.

Using the shift operators.

1:  /* Demonstrating the shift operators. */
2:
3:  #include <stdio.h>
4:
5:  main()
6:  {
7:       unsigned int y, x = 255;
8:       int count;
9:
10:      printf("Decimal\t\tshift left by\tresult\n");
11:
12:      for (count = 1; count < 8; count++)
13:      {
14:          y = x << count;
15:          printf("%d\t\t%d\t\t%d\n", x, count, y);
16:      }
17:      printf("\n\nDecimal\t\tshift right by\tresult\n");
18:
19:      for (count = 1; count < 8; count++)
20:      {
21:           y = x >> count;
22:           printf("%d\t\t%d\t\t%d\n", x, count, y);
23:      }
24:      return(0);
25: }
Decimal         shift left by   result
255             1               254
255             2               252
255             3               248
255             4               240
255             5               224
255             6               192
255             7               128
Decimal         shift right by  result
255             1               127
255             2               63
255             3               31
255             4               15
255             5               7
255             6               3
255             7               1

C Home | All C Tutorials | Latest C Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(9)


1. View Comment

The result shown in the left shift is wrong...

View Tutorial          By: krishnalal.K.K at 2008-05-21 03:05:07
2. View Comment

This is first time i am using this net

View Tutorial          By: selvam at 2008-08-19 00:04:44
3. View Comment

Yes Left shift result is wrong its like
510
1020
2040
4080
8160
16320
32640


View Tutorial          By: Ragunath k at 2010-05-10 04:45:33
4. View Comment

510
1020
2040
4080
8160
16320
32640 why are wrong????????????????


View Tutorial          By: sky at 2010-07-17 06:54:46
5. View Comment

this is good, but i need a simple program about shift operater .

View Tutorial          By: ramalakshmi at 2011-01-05 03:38:26
6. View Comment

left shift output is wrong, for this results so many people get confused

View Tutorial          By: naga sudha at 2011-04-20 04:26:40
7. View Comment

how to multiply 2.54*0.7071 using only bitwise operator in C

View Tutorial          By: vindya at 2012-01-12 07:23:08
8. View Comment

The example shown in this article uses 8-bit operation. So, I think the results are true

View Tutorial          By: wisnu at 2012-10-04 05:11:51
9. View Comment

i need help in writing code for multiplication program in C using bitwise operation : i got for 2,3,4,5 and 8,9 but not able to find for 6th and 7th multiplication: becoz this type logic is working for 6th and 7th table with: "result = (X <<1)+X ;" (while increment the number from 2 to 3 : the table is shifting from 5 to 9 th table. so please help me in this. Thanks in advance.

View Tutorial          By: viswanath at 2013-02-23 14:13:40

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by Fazal
The Tag Life Cycle with Attributes in JSP
Using Shift Operators in C
IncludeAction in Struts
typedef example program in C
Arithmetic Evaluation Using the Expression Language in JSP
Tutorial on Complicated Declarations in C
Static Variables in C
getPosition() the right strategy in getting GPS
Precedence and Order of Evaluation in C
File Copying in C
JSF Life Cycle
Getting User Input Using cin in C++
UDP Datagram sample in J2ME
GUI components and menu based J2ME Applications.
A Serialization Example in Java

More Tutorials in C
Sum of the elements of an array in C
Printing a simple histogram in C
Sorting an integer array in C
Find square and square root for a given number in C
Simple arithmetic calculations in C
Command-line arguments in C
Calculator in C
Passing double value to a function in C
Passing pointer to a function in C
Infix to Prefix And Postfix in C
while, do while and for loops in C
Unicode and UTF-8 in C
Formatting with printf in C
if, if...else and switch statements in C with samples
Statements in C

More Latest News
Most Viewed Articles (in C )
Using memset(), memcpy(), and memmove() in C
lseek() sample program in C
perror() Function - example program in C
UNIX read and write system calls sample program in C
assert() Function Example program in C
Find square and square root for a given number in C
Printing a simple histogram in C
Arrays of Structures example program in C
scanf and sscanf sample program in C
Listing Files and Directories sample program in C
Character Arrays in C
External Variables and Scope in C
goto and labels in C
Open, Creat, Close, Unlink system calls sample program in C
Using calloc() Function in C
Most Emailed Articles (in C)
scanf and sscanf sample program in C
Line Counting sample program in C
union example program in C
Variables and Arithmetic Expressions in C
Assignment Operators and Expressions in C
Command-line Arguments in C
Error Handling - Stderr and Exit - sample program in C
A Storage Allocator sample program in C
Using memset(), memcpy(), and memmove() in C
Writing The First C program
Infix to Prefix And Postfix in C
Getting Started with C
File Copying in C
Arrays sample program in C
Constants and escape sequences in C