Language: EN

cpp-operadores-bitwise

Bitwise Operators in C++

Bit and byte operators in C++ allow you to manipulate data at the bit level of individual bits or groups of bits in a variable.

Unlike common arithmetic operators, which operate on whole values, bit operators manipulate each bit in the binary representation of a number.

C++ provides several bitwise operators, each with its own specific functionality. Here is a summary table.

OperatorDescription
&Bitwise AND
|Bitwise OR
^Bitwise XOR
~Bitwise NOT
<<Left shift
>>Right shift

Types of Bitwise Operators in C++

AND Operator (&)

The AND bitwise operator performs a logical AND operation between each pair of corresponding bits of two operands. The result is 1 only if both bits are 1, otherwise the result is 0.

int a = 6;   // Binary: 00000110
int b = 3;   // Binary: 00000011

int result = a & b; // Binary: 00000010 (only the common bits)

In this example, the operation a & b compares each bit in the numbers a and b. Only the third bit from the right is 1 in both, so the result is 00000010, which equals 2 in decimal.

OR Operator (|)

The OR bitwise operator performs a logical OR operation between each pair of corresponding bits of two operands. The result is 1 if at least one of the bits is 1, otherwise the result is 0.

int a = 6;   // Binary: 00000110
int b = 3;   // Binary: 00000011

int result = a | b; // Binary: 00000111 (if any bit is 1)

Here, the operation a | b sets each bit to 1 if at least one of the corresponding bits in a or b is 1. The result is 00000111, which is 7 in decimal.

XOR Operator (^)

int a = 6;   // Binary: 00000110
int b = 3;   // Binary: 00000011

int result = a ^ b; // Binary: 00000101 (only if the bits are different)
}

In this case, a ^ b returns 1 only if the corresponding bits in a and b are different. The result 00000101 equals 5.

NOT Operator (~)

The NOT bitwise operator performs a bitwise negation of its operand, inverting each bit of the operand (the 0s become 1s and vice versa).

int a = 6;    // Binary: 00000110

int result = ~a; // Binary: 11111001 in an 8-bit representation
}

Left Shift

The << operator shifts all bits to the left. In the example, 3 << 2 is 00001100, which equals 12.

int a = 3;   // Binary: 00000011
int result = a << 2; // Shifts two bits to the left
// Result: 12 (00001100)
}

This shift is similar to multiplying the number by powers of 2 (in this case, 3 * 2^2 = 12).

Right Shift

The >> operator shifts the bits to the right. In the example, 12 >> 2 is 00000011, which equals 3.

int a = 12;  // Binary: 00001100
int result = a >> 2; // Shifts two bits to the right

// Result: 3 (00000011)

This shift is similar to dividing the number by powers of 2 (12 / 2^2 = 3).

Applications of Bit Operators

Bit Manipulation to Apply Masks

Using masks allows us to manipulate specific bits without affecting others. For example, to set the third bit of a number to 1:

int number = 5;     // Binary: 00000101
int mask = 1 << 2;  // Mask: 00000100
    
number = number | mask;  // Apply bitwise OR to set the bit
// Result: 7 (00000111)

Bit Checking

To check if a specific bit is set (for example, the third bit):

int number = 5;         // Binary: 00000101
int mask = 1 << 2;      // Mask: 00000100

bool isThirdHigh = number & mask   // Checks if the third bit is 1
}

Using Bit and Byte Operators

Suppose we want to activate the third bit of a variable and deactivate the fourth bit. We could use bit operators as follows:

int value = 12;  // Binary representation: 1100

value |= (1 << 2);  // Activate third bit (counting from 0)
value &= ~(1 << 3); // Deactivate fourth bit
}

In this example, we are using bit operators to perform precise operations on the third and fourth bits of the value variable.

If you want to delve deeper, here’s a binary course👇