Bitwise Operators
AND - & (Bitwise AND)
- Operation: Performs the AND operation on each pair of corresponding bits of two numbers. The result is 1 only if both bits are 1; otherwise, it results in 0.
- Truth Table:
0 & 0 = 00 & 1 = 01 & 0 = 01 & 1 = 1
Example
int a = 5; // Binary: 0101int b = 3; // Binary: 0011int result = a & b; // Performs bitwise AND: 0101 & 0011 = 0001 (Decimal: 1)cout << "a & b = " << result << endl;
OR - | (Bitwise OR)
- Operation: Performs the OR operation on each pair of corresponding bits. The result is 1 if at least one of the bits is 1; otherwise, it results in 0.
- Truth Table:
0 | 0 = 00 | 1 = 11 | 0 = 11 | 1 = 1
Example
int a = 5; // Binary: 0101int b = 3; // Binary: 0011int result = a | b; // Performs bitwise OR: 0101 | 0011 = 0111 (Decimal: 7)cout << "a | b = " << result << endl;
NOT - ~ (Bitwise NOT)
- Operation: Inverts the bits of the number. The bitwise NOT flips each bit: 0 becomes 1 and 1 becomes 0. It works on a single operand (unary operator).
- Truth Table:
~0 = 1~1 = 0
Example
int a = 5; // Binary: 0101int result = ~a; // Performs bitwise NOT: ~0101 = 1010 (Decimal: -6 in two's complement)cout << "~a = " << result << endl;
XOR - ^ (Bitwise XOR)
- Operation: Performs the XOR operation on each pair of corresponding bits. The result is 1 if the bits are different, and 0 if they are the same
- Truth Table:
0 ^ 0 = 00 ^ 1 = 11 ^ 0 = 11 ^ 1 = 0
Example
int a = 5; // Binary: 0101int b = 3; // Binary: 0011int result = a ^ b; // Performs bitwise XOR: 0101 ^ 0011 = 0110 (Decimal: 6)cout << "a ^ b = " << result << endl;
Left Shift Operator (<<)
- Operation: The left shift operator shifts the bits of a number to the left by a specified number of positions. Each shift to the left effectively multiplies the number by 2 for each position shifted.
- Syntax:
value << num_of_positions;
Example
int a = 5; // Binary: 0101int result = a << 1; // Shifts bits of 5 (0101) one position left: 1010 (binary for 10)cout << "a << 1 = " << result << endl;
Right Shift Operator (>>)
- Operation: The right shift operator shifts the bits of a number to the right by a specified number of positions. Each shift to the right divides the number by 2 for each position shifted.
- Syntax:
value >> num_of_positions;
Example
int a = 10; // Binary: 1010int result = a >> 1; // Shifts bits of 10 (1010) one position right: 0101 (binary for 5)cout << "a >> 1 = " << result << endl;
Converting Decimal to Binary
To convert a decimal number to binary, repeatedly divide the number by 2 and record the remainders. Then, read the remainders in reverse order (bottom to top).
Steps:
- Divide the number by 2.
- Record the remainder (0 or 1).
- Continue dividing the quotient by 2 until the quotient is 0.
- The binary representation is the remainders read in reverse order.
Example: Convert 13 (decimal) to binary:
13 ÷ 2 = 6, remainder = 1 6 ÷ 2 = 3, remainder = 0 3 ÷ 2 = 1, remainder = 1 1 ÷ 2 = 0, remainder = 1
Read remainders bottom to top: 1101
Converting Binary to Decimal
To convert a binary number to decimal, multiply each bit by 2 raised to the power of its position (starting from 0 from the rightmost bit) and then sum all the results.
Steps:
- Start from the rightmost bit, which is position 0.
- Multiply each bit by 2 position.
- Sum the results to get the decimal value.
Example: Convert 1101 (binary) to decimal:
1 × 2³ = 81 × 2² = 40 × 2¹ = 01 × 2⁰ = 1
Sum = 8 + 4 + 0 + 1 = 13
Two’s Complement and Negative Numbers in Binary
Two’s Complement Representation:
- Two’s complement is a method for representing negative numbers in binary.
- Most Significant Bit (MSB):
- 0 = positive number.
- 1 = negative number.
- The two’s complement is found by:
- Flip all bits (one’s complement).
- Add 1 to the result.
Converting Negative Decimal to Binary (Two’s Complement)
Steps:
- Convert the positive value of the number to binary.
- Pad the binary to the desired bit length (commonly 8, 16, 32 bits).
- Flip all bits (one’s complement).
- Add 1 (two’s complement).
Example: Convert -13 (decimal) to 8-bit binary:
- Positive 13 in binary: 00001101 (8-bit).
- Flip all bits: 11110010 (one’s complement).
- Add 1: 11110011 (two’s complement).
Result: -13 (decimal) = 11110011 (binary).
Converting Binary (Two’s Complement) to Negative Decimal
Steps:
- Check the MSB:
- If 1, it’s a negative number.
- Flip all bits (one’s complement).
- Add 1 to get the absolute value.
- Add the negative sign.
Example: Convert 11110011 (8-bit binary) to decimal:
- MSB is 1, so it’s negative.
- Flip all bits: 00001100.
- Add 1: 00001101 = 13 (decimal).
Result: -13 (decimal).