- Related Questions & Answers
- Write you own Power without using multiplication(*) and division(/) operators in C Program
- Inplace Operators in Python - ixor(), iand(), ipow()
- Inplace Operators in Python - iadd(), isub(), iconcat()
- Python Increment and Decrement Operators
- # and ## Operators in C ?
- Can we combine MySQL IN AND LIKE operators?
- What are the restrictions on increment and decrement operators in java?
- What are the main shift operators provided by Java? Explain with an example?
- Explain JavaScript Bitwise NOT, Left shift and Right shift?
- Explain about bitwise operators in JavaScript?
- Write the importance of shift() method in javascript array?
- What types of logical operators are in javascript?
- what is the main difference between '=' and '==' operators in javascript?
- Write the main difference between '==' and '===' operators in javascript?
- Increment and Decrement Operators in Python?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

The bitwise shift operators are the right-shift operator (>>), which moves the bits of shift_expression to the right, and the left-shift operator (<<), which moves the bits of shift_expression to the left.

The left-shift operator causes the bits in shift-expression to be shifted to the left by the number of positions specified by additive-expression. The bit positions that have been vacated by the shift operation are zero-filled. A left shift is a logical shift (the bits that are shifted off the end are discarded, including the sign bit).

The right-shift operator causes the bit pattern in shift-expression to be shifted to the right by the number of positions specified by additive-expression. For unsigned numbers, the bit positions that have been vacated by the shift operation are zero-filled. For signed numbers, the sign bit is used to fill the vacated bit positions. In other words, if the number is positive, 0 is used, and if the number is negative, 1 is used.

#include<iostream> using namespace std; int main() { int a = 1, b = 3; // a right now is 00000001 // Left shifting it by 3 will make it 00001000, ie, 8 a = a << 3; cout << a << endl; // Right shifting a by 2 will make it 00000010, ie, 2 a = a >> 2; cout << a << endl; return 0; }

This will give the output −

8 2

Note that these operators behave very differently with negative numbers. The result of a right-shift of a signed negative number is implementation-dependent. If you left-shift a signed number so that the sign bit is affected, the result is undefined.

There are also 2 complex operators that can be used to assign the value directly to the value on left. These are the <<= operator and the >>= operator.

Refer to https://msdn.microsoft.com/en-us/library/336xbhcz.aspx for a much detailed inspection of the shift operators.

Advertisements