
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are Shift Operators in C++
In C++, bitwise operators are used to perform operations on binary numbers. Since computers store all data in the form of binary (0s and 1s), therefore, every value, like decimal numbers, characters, and booleans, is internally represented in binary.
for example: 5 = 00000101 and 3 = 00000011
To learn more about how to convert these decimal values to binary, you can visit this page: Decimal to Binary Conversion.
Shift Operators in C++
The shift operator is one of the types of bitwise operators, which are used to move bits of a number left or right.
There are two types of bitwise shift operators.
Left-Shift Operator in C++
The left shift operator causes the bits to move or shift to the left by the number of positions specified by the user. And after the bits are shifted, the remaining vacant positions on the right are filled with 0s.
A left shift is a logical shift, which means that any bits that move out from the left side are simply discarded.
It does not even preserve the sign bit (since negative numbers are represented with a leading 1 and positive numbers with a leading 0); therefore, shifting can change the sign and value type of a number, especially for signed integers.
Syntax
Here is the following syntax for the left shift operator.
value << position_count;
Here, value represents the integer value whose bits you want to shift.
position_count represents the number of positions you want to shift to the left side.
Example
#include <iostream> #include <bitset> using namespace std; int main() { int num1 = 5; int result1 = num1 << 1; // Left shift by 1 bit cout << "Original number: " << num1 << endl; cout << "After left shift by 1: " << result1 << endl; signed char num = 64; signed char result = num << 1; // left shift by 1 bit cout << "Original number: " << (int)num << ", Binary: " << bitset<8>(num) << endl; cout << "After left shift: " << (int)result << ", Binary: " << bitset<8>(result) << endl; return 0; }
Output
Original number: 5 After left shift by 1: 10 Original number: 64, Binary: 01000000 After left shift: -128, Binary: 10000000
Right-Shift Operator in C++
Similarly, the right shift operator moves the bits of a number to the right by the number of positions specified by the user. Here also, the remaining vacant positions on the left (which were created by shifting bits to the right) are filled with 0s.
But for signed numbers;
- If the number is positive, then it will fill the 0s from the left.
- If the number is negative, then it will fill the 1s from the left to preserve the sign; this behavior is also known as arithmetic (or signed) shift.
Syntax
Here is the following syntax for the left shift operator.
value >> position_count;
Here, value represents the integer value whose bits you want to shift.
position_count represents the number of positions you want to shift to the right side.
Example
#include <iostream> #include <bitset> using namespace std; int main() { int num1 = 20; int result1 = num1 >> 1; // Right shift by 1 bit cout << "Original number: " << num1 << endl; cout << "After right shift by 1: " << result1 << endl; signed char num = -64; signed char result = num >> 1; // Right shift by 1 bit cout << "Original number: " << (int)num << ", Binary: " << bitset<8>(num) << endl; cout << "After right shift: " << (int)result << ", Binary: " << bitset<8>(result) << endl; return 0; }
Output
Original number: 20 After right shift by 1: 10 Original number: -64, Binary: 11000000 After right shift: -32, Binary: 11100000
There are also 2 complex operators, which can be used to assign the value directly to the value on the left. These are the <<= and the >>= operators.
For a more detailed inspection of the shift operators, you can refer to https://msdn.microsoft.com/en-us/library/336xbhcz.aspx.