Bitwise operators are used to perform operations on individual bits of numbers. They are useful when you want to manipulate data at the bit level.
&
- Bitwise AND|
- Bitwise OR^
- Bitwise XOR (Exclusive OR)~
- Bitwise NOT<<
- Left Shift>>
- Right Shift
This operator compares each bit of the first operand to the corresponding bit of the second operand. If both bits are 1
, the corresponding result bit is set to 1
. Otherwise, the corresponding result bit is set to 0
.
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int result = a & b; // 0001 in binary which is 1 in decimal
This operator compares each bit of the first operand to the corresponding bit of the second operand. If either bit is 1
, the corresponding result bit is set to 1
. Otherwise, the corresponding result bit is set to 0
.
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int result = a | b; // 0111 in binary which is 7 in decimal
This operator compares each bit of the first operand to the corresponding bit of the second operand. If the bits are different, the corresponding result bit is set to 1
. Otherwise, it's set to 0
.
Example:
int a = 5; // 0101 in binary
int b = 3; // 0011 in binary
int result = a ^ b; // 0110 in binary which is 6 in decimal
This operator inverts the bits of its operand.
Example:
int a = 5; // 0101 in binary
int result = ~a; // 1010 in binary which is -6 in decimal (due to two's complement representation)
This operator shifts the left operand's value to the left by the number of bits specified by the right operand.
Example:
int a = 5; // 0101 in binary
int result = a << 1; // 1010 in binary which is 10 in decimal
This operator shifts the left operand's value to the right by the number of bits specified by the right operand.
Example:
int a = 5; // 0101 in binary
int result = a >> 1; // 0010 in binary which is 2 in decimal
Bitwise operators are powerful tools for manipulating individual bits in numbers. They are commonly used in low-level programming, such as systems programming and embedded systems. Understanding these operators can help you optimize your code and perform operations that aren't possible with regular arithmetic operators.
Note: Always be careful when using bitwise operators, as incorrect usage can lead to unexpected results.