← Home ← Codex ← DEBT
Browse by Category
+ added · updated 7d
← Back to glossary

Bit Manipulation

Algorithms Intermediate
debt(d7/e3/b3/t7)
d7 Detectability Operational debt — how invisible misuse is to your safety net

Closest to 'only careful code review or runtime testing' (d7). The detection_hints list phpstan as the tool, but automated detection is explicitly marked 'no'. PHPStan can catch type mismatches but won't flag misuse of | vs & for flag-checking or subtle bitmask logic errors — these typically surface only during code review or when bugs manifest in production (wrong error levels, incorrect PDO fetch modes, unexpected permission behaviour).

e3 Effort Remediation debt — work required to fix once spotted

Closest to 'simple parameterised fix' (e3). The quick_fix describes replacing boolean column arrays or array_intersect checks with bitwise integer operations. This is more than a single-line swap (it may touch schema, queries, and flag definitions) but is generally contained within one component or feature area — not a cross-cutting refactor.

b3 Burden Structural debt — long-term weight of choosing wrong

Closest to 'localised tax' (b3). The applies_to covers web and cli contexts broadly, but bitmask usage is typically isolated to specific permission systems, feature-flag modules, or calls to PHP stdlib functions like error_reporting and PDO. The rest of the codebase is largely unaffected unless bitmask integers are shared as a central permission model.

t7 Trap Cognitive debt — how counter-intuitive correct behaviour is

Closest to 'serious trap' (t7). The misconception field directly names the canonical wrong belief: developers assume bitmasks are only for low-level systems programming and misread PHP's own stdlib. The common_mistakes reinforce multiple dangerous confusions: using | instead of & for flag checks, misunderstanding falsy 0, confusing ~ with !, and integer overflow on 32-bit PHP. These contradict intuitions from boolean/array logic, earning a t7.

About DEBT scoring →

Also Known As

bitmask bitwise operators bitwise flags

TL;DR

Using bitwise operators (AND, OR, XOR, NOT, shifts) to manipulate individual bits — enabling compact storage, fast arithmetic, and O(1) set operations.

Explanation

Bitwise operators in PHP: & (AND), | (OR), ^ (XOR), ~ (NOT), << (left shift), >> (right shift). Common patterns: checking a bit (n & (1<<i)), setting a bit (n | (1<<i)), clearing a bit (n & ~(1<<i)), toggling a bit (n ^ (1<<i)). Applications: permission flags (user roles as bitmask), feature flags, fast powers of 2, parity checking, and space-efficient sets. Bitmasks are used in PHP's own API — error_reporting levels, PDO fetch modes, and PREG flags.

Watch Out

In PHP, & has lower operator precedence than == — the expression $x & FLAG == 1 evaluates as $x & (FLAG == 1), not ($x & FLAG) == 1. Always parenthesise bitwise checks.

Common Misconception

Bit manipulation is only for low-level systems programming — PHP uses bitmasks throughout its standard library; understanding them is needed to use error_reporting, PDO fetch modes, and file permissions correctly.

Why It Matters

PHP's own error_reporting, PDO::FETCH_*, and file permission octals all use bitmasks — misusing them produces subtle bugs like wrong error levels or incorrect fetch modes.

Common Mistakes

  • Using | to check if a flag is set — use & for checking: if ($flags & MY_FLAG) not if ($flags | MY_FLAG).
  • Not understanding that 0 is falsy — if ($flags & FLAG) is false when FLAG is not set AND when $flags is 0.
  • Integer overflow with large bitmasks in 32-bit PHP — use PHP_INT_SIZE to check word size.
  • Confusing ~ (bitwise NOT) with ! (logical NOT).

Avoid When

  • Avoid bitmasks when the set of flags is large, changes often, or needs to be queried individually in SQL — a junction table is more maintainable.
  • Do not use bitwise operators where boolean operators are intended — & vs && and | vs || have different short-circuit behaviour and precedence.
  • Avoid bit manipulation in domain logic where clarity matters more than micro-optimisation — future maintainers will not thank you.

When To Use

  • Use bitmasks to store multiple boolean flags in a single integer column — compact, fast to query, and easy to extend without schema changes.
  • Apply bitwise operations for performance-critical tight loops: power-of-two checks, fast modulo, flag testing in inner loops.
  • Use XOR for in-place swaps and simple checksums where readability is secondary to performance.

Code Examples

💡 Note
The bad example uses | (OR) to test a flag — it always returns non-zero and the check is always true. The fix uses & (AND) to mask the value and check whether that specific bit is set.
✗ Vulnerable
// Wrong operator for flag check — | always returns non-zero:
define('CAN_READ',   0b001); // 1
define('CAN_WRITE',  0b010); // 2
define('CAN_DELETE', 0b100); // 4

$permissions = CAN_READ; // User has read only
if ($permissions | CAN_WRITE) { // Bug: | always non-zero if either is set
    allowWrite(); // Always executes!
}
✓ Fixed
// Correct bitmask operations:
$permissions = CAN_READ | CAN_WRITE; // 0b011 = 3

// Check:
if ($permissions & CAN_WRITE)  { /* Has write permission */ }
if (!($permissions & CAN_DELETE)) { /* Does NOT have delete */ }

// PHP stdlib bitmask:
error_reporting(E_ALL & ~E_NOTICE); // All errors except notices
$stmt = $pdo->query($sql, PDO::FETCH_ASSOC | PDO::FETCH_UNIQUE);

Added 15 Mar 2026
Edited 31 Mar 2026
Views 43
Rate this term
No ratings yet
🤖 AI Guestbook educational data only
| |
Last 30 days
0 pings T 1 ping W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 2 pings S 0 pings M 0 pings T 1 ping W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 0 pings T 0 pings W 1 ping T 0 pings F 1 ping S 0 pings S 0 pings M 1 ping T 0 pings W
No pings yet today
PetalBot 1
Amazonbot 7 Perplexity 4 Ahrefs 4 SEMrush 3 Google 3 Unknown AI 2 Claude 2 ChatGPT 2 Bing 2 Scrapy 2 Meta AI 1 PetalBot 1
crawler 30 crawler_json 3
DEV INTEL Tools & Severity
🟢 Low ⚙ Fix effort: Medium
⚡ Quick Fix
Use bitwise operations for permission flags and feature toggles — a single integer can store 64 boolean flags and operations are O(1) vs array lookups
📦 Applies To
any web cli
🔗 Prerequisites
🔍 Detection Hints
65+ boolean columns for permissions; permission check with array_intersect when bitwise AND would work; PHP & | ^ << >> operators avoided
Auto-detectable: ✗ No phpstan
⚠ Related Problems
🤖 AI Agent
Confidence: Low False Positives: Medium ✗ Manual fix Fix: Medium Context: Function Tests: Update


✓ schema.org compliant