Using Shift Operators in C
By: Fazal in C Tutorials on 2007-10-03
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
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Sum of the elements of an array in C
Printing a simple histogram in C
Find square and square root for a given number in C
Simple arithmetic calculations 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
Comments