Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Questions | Answers | Discussions | Knowledge sharing | Communities & more.
In C programming, bitwise operators are a powerful tool for manipulating data at the bit level. Unlike regular arithmetic operators that work on whole numbers, bitwise operators work on individual bits (0s and 1s) of binary data. This allows for fine-grained control and efficient operations in specific programming scenarios.
Here’s a breakdown of the common bitwise operators in C:
1. Bitwise AND (&):
Performs a bitwise AND operation on two operands.
The result of each bit position is 1 only if the corresponding bits in both operands are 1. Otherwise, the result bit is 0.
Example:
C
int a = 6 (binary: 0110);
int b = 4 (binary: 0100);
int c = a & b; // c will be 4 (binary: 0100)
0110
& 0100
——-
0100
Use code with caution.
content_copy
2. Bitwise OR (|):
Performs a bitwise OR operation on two operands.
The result of each bit position is 1 if either (or both) of the corresponding bits in the operands are 1. Otherwise, the result bit is 0.
Example:
C
int a = 6 (binary: 0110);
int b = 4 (binary: 0100);
int c = a | b; // c will be 6 (binary: 0110)
0110
| 0100
——-
0110
Use code with caution.
content_copy
3. Bitwise XOR (^):
Performs a bitwise XOR (exclusive OR) operation on two operands.
The result of each bit position is 1 if the corresponding bits in the operands are different. If they are the same, the result bit is 0.
Example:
C
int a = 6 (binary: 0110);
int b = 4 (binary: 0100);
int c = a ^ b; // c will be 2 (binary: 0010)
0110
^ 0100
——-
0010
Use code with caution.
content_copy
4. Bitwise NOT (~):
Performs a bitwise NOT operation on a single operand.
Inverts all the bits in the operand. A 0 becomes 1 and vice versa.
Example:
C
int a = 6 (binary: 0110);
int c = ~a; // c will be -7 (binary: 1001) with the sign bit set due to two’s complement)
~0110
——-
1001
Use code with caution.
content_copy
5. Left Shift (<<):
Shifts the bits of the left operand to the left by the number of positions specified by the right operand.
Zeros are filled in on the right side.
Example:
C
int a = 6 (binary: 0110);
int c = a << 1; // c will be 12 (binary: 1100)
0110 <>):
Shifts the bits of the left operand to the right by the number of positions specified by the right operand.
Behavior for right shift depends on the sign bit of the operand:
Unsigned right shift: Zeros are filled in on the left side.
Signed right shift: The sign bit is replicated on the left side (for preserving the sign of negative numbers).
Example (Unsigned right shift):
C
int a = 20 (binary: 0001 0100);
int c = a >> 2; // c will be 5 (binary: 0000 0101)
0001 0100 >> 2
——-
0000 0101
Use code with caution.
content_copy
Applications of Bitwise Operators:
Bit Masking: Isolating specific bits in a data value.
Flag Manipulation: Setting or checking individual flags in a bit field.
Low-level Programming: Interacting with hardware registers or memory manipulation.
Optimization: Sometimes, bitwise operations can be more efficient than arithmetic operations for specific tasks.