PHP

⚙️ Bitwise Assignment Operators in PHP

Bitwise Assignment Operators perform bit-level logical or shift operations on a variable and immediately assign the updated binary evaluation back to that same variable.

  • Shorthand Formula: The expression $a OP= $b; is functionally equivalent to $a = $a OP $b; where OP represents any binary bitwise operator.

🛠️ How Bitwise Assignment Operators Work

Consider two initial integer values converted into their 8-bit binary representations:

$a = 22;  ➔  00010110
$b = 3;   ➔  00000011

🔍 PHP Bitwise Assignment Syntax and Code Examples

1️⃣ Bitwise AND Assignment (&=)
Performs a bitwise AND operation on two variables and assigns the result back to the left variable. Only matching 1 bits remain.

PHP
$a = 22; // 00010110
$b = 3;  // 00000011

$a &= $b; // Equivalent to: $a = $a & $b;
// 00010110 & 00000011 = 00000010 (Decimal: 2)

echo $a; // Outputs: 2

2️⃣ Bitwise OR Assignment (|=)
Performs a bitwise OR operation and assigns the result back. Preserves any bit that is 1 in either operand.

PHP
$a = 22; // 00010110
$b = 3;  // 00000011

$a |= $b; // Equivalent to: $a = $a | $b;
// 00010110 | 00000011 = 00010111 (Decimal: 23)

echo $a; // Outputs: 23

3️⃣ Bitwise XOR Assignment (^=)
Performs a bitwise XOR operation and assigns the result back. Sets the bit to 1 if bits are different, and 0 if identical.

PHP
$a = 22; // 00010110
$b = 3;  // 00000011

$a ^= $b; // Equivalent to: $a = $a ^ $b;
// 00010110 ^ 00000011 = 00010101 (Decimal: 21)

echo $a; // Outputs: 21

4️⃣ Left Shift Assignment (<<=)
Shifts bits to the left by the specified steps and assigns the updated value back. Each shift position doubles the integer value ($a \times 2^n$).

PHP
$a = 22; // 00010110

$a <<= 3; // Equivalent to: $a = $a << 3;
// 00010110 << 3 = 10110000 (Decimal: 176)

echo $a; // Outputs: 176

5️⃣ Right Shift Assignment (>>=)
Shifts bits to the right by the specified steps and assigns the updated value back. Each shift position halves the integer value ($\lfloor a / 2^n \rfloor$).

PHP
$a = 22; // 00010110

$a >>= 3; // Equivalent to: $a = $a >> 3;
// 00010110 >> 3 = 00000010 (Decimal: 2)

echo $a; // Outputs: 2

💡 Practical Applications

  • Permission Flags: Updating user permissions dynamically (e.g., granting a role via $user_perms |= READ_PRIVILEGE; or revoking via $user_perms &= ~WRITE_PRIVILEGE;).
  • Low-Level Data Manipulation: Efficiently modifying protocol headers, bit fields, or binary streams without temporary variables.
  • High-Performance Arithmetic: Executing fast inline multiplications or divisions by factors of two inside loops.

📊 Bitwise Assignment Operators Reference Table

Operator Name Shorthand Full Equivalent Description
&= Bitwise AND Assignment $a &= $b $a = $a & $b Performs bitwise AND and assigns result to $a.
|= Bitwise OR Assignment $a |= $b $a = $a | $b Performs bitwise OR and assigns result to $a.
^= Bitwise XOR Assignment $a ^= $b $a = $a ^ $b Performs bitwise XOR and assigns result to $a.
<<= Left Shift Assignment $a <<= $b $a = $a << $b Shifts bits left by $b positions and assigns result to $a.
>>= Right Shift Assignment $a >>= $b $a = $a >> $b Shifts bits right by $b positions and assigns result to $a.
  • 📝 Short Summary

  • &= keeps common 1 bits, |= keeps any 1 bit, and ^= keeps differing bits.
  • <<= shifts bits left (multiplies value), while >>= shifts bits right (divides value).
  • Bitwise assignment operators combine computation and reassignment into a single concise expression.