Codacoda
Back to Academy

bitwise operations

Check/Set/Clear/Toggle Bit

The four fundamental single-bit operations let you inspect and modify individual bits at any position. Check uses AND with a shifted 1, Set uses OR, Clear uses AND with NOT, and Toggle uses XOR. These are the building blocks of all higher-level bit manipulation.

Use Cases

  • Managing boolean arrays compactly with a single integer
  • Setting and reading hardware control register flags
  • Toggling feature states without branching
  • Bitmap-based memory allocators

Complexity Analysis

MetricBestAverageWorst
TimeO(1)O(1)O(1)
SpaceO(1)

Visualization

Value50000010176543210
Speed:1x
Starting value: 5 (00000101). We will check, set, clear, and toggle individual bits.Step 1 / 5

Implementation

Output

Click "Run Code" to see output...