Compound assignment Operators:
- Compound assignment operators reduce the size of modify expression.
- Operators are +=, -=, *=, &&=, >>=
A+=10 -> A=A+10 A+=B -> A=A+B X*=Y -> X=X*Y | Balance += Deposit -> Balance = Balance + Deposit Balance -= Withdraw -> Balance = Balance – Withdraw |
Bitwise operators:
- These operators convert the input into binary data and perform operations.
- Operators are,
- Bitwise AND (&)
- Bitwise OR (|)
- Bitwise XOR (^)
- The following table explains the how Bitwise operations returns the results.
Truth table:
A | B | A&B | A|B | A^B |
1 | 1 | 1 | 1 | 0 |
1 | 0 | 0 | 1 | 1 |
0 | 1 | 0 | 1 | 1 |
0 | 0 | 0 | 0 | 0 |
Note: After performing bitwise operations, results will be displayed in Decimal format only.
Bitwise – AND operator:
#include <stdio.h> int main() { int a = 12, b = 25; printf(“Output = %d”, a & b); return 0; } | 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) 00001100 & 00011001 ———- 00001000 = 8 (In decimal) |
Bitwise – OR operator:
#include <stdio.h> int main() { int a = 12, b = 25; printf(“Output = %d”, a | b); return 0; } | 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) 00001100 | 00011001 ———- 00011101 = 29 (In decimal) |
Bitwise – XOR operator:
#include <stdio.h> int main() { int a = 12, b = 25; printf(“Output = %d”, a ^ b); return 0; } | 12 = 00001100 (In Binary) 25 = 00011001 (In Binary) 00001100 ^ 00011001 ———– 00010101 = 21 (In decimal) |
Bitwise Complement Operator (~): It changes 1 to 0 and 0 to 1.
For example:
35 = 00100011 (In Binary) | ~ 00100011 ———- 11011100 = 220 (In decimal) |
The bitwise complement of 35 (~35) is -36 instead of 220, but why?
Answer: It is the value of 2’s complement
2’s Complement:
- The 2’s complement of a number is equal to the complement of that number plus 1.
- Bitwise Complement of Any Number N is -(N+1).
Decimal | Binary | 2’s complement |
0 1 12 220 | 00000000 00000001 00001100 11011100 | -(11111111+1) = -00000000 = -0(decimal) -(11111110+1) = -11111111 = -256(decimal) -(11110011+1) = -11110100 = -244(decimal) -(00100011+1) = -00100100 = -36(decimal) |
Formula of 2’s complement:
Bitwise complement of N = ~N (represented in 2’s complement form) 2’complement of ~N= -(~(~N)+1) = -(N+1) |
Program to understand Bitwise complement operator:
#include <stdio.h> int main() { printf(“Output = %d\n”, ~35); printf(“Output = %d\n”, ~-12); return 0; } Output = -36 Output = 11 |