Selected Reading

C++ Bitset::operator<<= Function



The C++ std::bitset::operator<<= is a compound assignment operator that performs a bitwise left SHIFT operation on the current bitset object and then assigns the result back to the same bitset object.

The bitwise left SHIFT operator shifts the bits of the operand to the left by the specified number of positions. The leftmost bits of the bitset object are discarded and a 0 bit is appended to the right end of the resultant binary value. The number of times we need to append a 0 bit depends on the number of positions that the bits are shifted.

The compound assignment operators are formed by combining a bitwise operator with the assignment operator (=), resulting in a new operator that performs the bitwise operation and assigns the result to the left operand.

Syntax

Following is the syntax for std::bitset::operator<<= −

bitset& operator<<= (size_t pos);

Parameters

pos − Number of bits to be shifted.

Return value

Returns this pointer.

Example 1

The following example shows the usage of std::bitset::operator<<= on a single bitset operand.

Here, we are creating a bitset "b" of size "4" with the binary value "0001". Then, we are shifting the bits of "b" one position to the left using the left shift assignment operator.

#include <iostream>
#include <bitset>
using namespace std;

int main(void) {
   bitset<4> b("0001");
   b <<= 1;
   cout << b << endl;
   return 0;
}

Output

Let us compile and run the above program, this will produce the following result −

0010

Example 2

Here, we are doubling an integer value using operator<<=.

In the following example, we are creating an integer variable "x" with an initial value of "10". Then, we are using the left shift assignment operator to shift its bits to the left by one position, effectively doubling its value.

#include <iostream>
#include <bitset>
using namespace std;

int main(void) {
   int x = 10;
   x <<= 1;
   cout << x << endl;
   return 0;
}

Output

If we run the above code it will generate the following output −

20

Example 3

Now, we are setting a bit in a binary representation using operator<<=.

In the example given below, we are creating a 32-bit unsigned integer (non-negative values) variable called "mask" and assigning it with the hexadecimal value "0x01" (binary 0001). Then, we are using the left shift assignment operator to move the binary digits of the original value to the left by "3" positions, effectively multiplying it by 23.

#include <iostream>
#include <bitset>
using namespace std;

int main(void) {
   uint32_t mask = 0x01;
   mask <<= 3;
   cout << mask << endl;
   return 0;
}

Output

Following is an output of the above code −

8

Example 4

In here, we are shifting multiple values in an array to the left.

In the example below, we are creating an integer array "arr" with values "{16, 32, 64, 128}" (each value is formed by raising 2n). Then, we are shifting each element of the "arr" to the left by "4" bits using the left shift assignment operator.

#include <iostream>
#include <bitset>
using namespace std;

int main(void) {
   int arr[] = {16, 32, 64, 128};
   for (int i = 0; i < 4; i++) {
      // left shift by 4 bits
      arr[i] <<= 4;          
      cout << arr[i] << " ";  
   } 
   return 0;
}

Output

We can see that each value in the array is multiplied by 2 raised to the power of 4 (i.e. 16), effectively increasing the value by a factor of 16 in the following output −

256 512 1024 2048
Advertisements