PHP
⚙️ Bitwise Operators in PHP
Bitwise Operators in PHP perform evaluations and manipulations directly at the bit level (0s and 1s). Integers are converted into their binary representations, processed bit-by-bit using Boolean logic, and evaluated back to integer values.
-
Key Characteristics: Bitwise operators process integer values by evaluating corresponding binary positions. Except for Bitwise NOT (
~), which is a unary operator, all bitwise operations take two operands.
🛠️ How Bitwise Operators Work
Consider two sample integer values and their 8-bit binary equivalents:
$a = 22; ➔ 00010110
$b = 3; ➔ 00000011
🔍 PHP Bitwise Operators and Syntax
1️⃣ Bitwise AND (&)
Returns 1 only if both evaluated bits are 1; otherwise, returns 0.
$a = 22; // 00010110
$b = 3; // 00000011
$result = $a & $b;
// 00010110 & 00000011 = 00000010 (Decimal: 2)
echo $result; // Outputs: 2
2️⃣ Bitwise OR (|)
Returns 1 if at least one of the corresponding bits is 1.
$a = 22; // 00010110
$b = 3; // 00000011
$result = $a | $b;
// 00010110 | 00000011 = 00010111 (Decimal: 23)
echo $result; // Outputs: 23
3️⃣ Bitwise XOR (^)
Returns 1 if the compared bits are different, and 0 if they are identical.
$a = 22; // 00010110
$b = 3; // 00000011
$result = $a ^ $b;
// 00010110 ^ 00000011 = 00010101 (Decimal: 21)
echo $result; // Outputs: 21
4️⃣ Bitwise NOT (~)
Inverts all bits (1 becomes 0, and 0 becomes 1).
$a = 22; // 00010110
$result = ~$a;
// Inverts bits: 11101001
echo $result; // Note: Outputs two's complement signed integer (-23)
5️⃣ Left Shift (<<)
Shifts bits to the left by the specified number of positions and pads 0s to the right. Equivalent to multiplying by powers of 2 ($a \times 2^n$).
$a = 22; // 00010110
$result = $a << 3;
// 00010110 << 3 = 10110000 (Decimal: 176)
echo $result; // Outputs: 176
6️⃣ Right Shift (>>)
Shifts bits to the right by the specified number of positions. Equivalent to integer division by powers of 2 ($\lfloor a / 2^n \rfloor$).
$a = 22; // 00010110
$result = $a >> 3;
// 00010110 >> 3 = 00000010 (Decimal: 2)
echo $result; // Outputs: 2
💡 Common Use Cases
Bitwise operators are applied in specialized low-level contexts:
- Permission Flags: Authorization systems store multiple boolean settings into a single integer mask.
- Performance Optimization: High-frequency mathematical operations like bit masking and power-of-two arithmetic.
- Encryption & Compression: Data encoding, checksum hashing algorithms, and bit masking.
📊 Bitwise Operators Reference Table
| Operator | Name | Example | Description |
|---|---|---|---|
& |
Bitwise AND | $a & $b |
Bits that are 1 in both $a and $b are set to 1. |
| |
Bitwise OR | $a | $b |
Bits that are 1 in either $a or $b are set to 1. |
^ |
Bitwise XOR | $a ^ $b |
Bits that are 1 in either $a or $b, but not both, are set to 1. |
~ |
Bitwise NOT | ~$a |
Inverts all bits (0 to 1, 1 to 0). |
<< |
Shift Left | $a << $b |
Shifts bits of $a to the left by $b steps. |
>> |
Shift Right | $a >> $b |
Shifts bits of $a to the right by $b steps. |
-
📝 Short Summary
- Bitwise operators process numbers at their individual binary bit levels.
-
Shift operators (
<<,>>) allow fast multiplication and division by powers of 2. -
Bitwise NOT (
~) results depend on signed integer representation in 64-bit PHP architecture.
