Overview
Bitwise operators perform logical operations on corresponding bits of integer values. Bitmasking uses these operators to represent multiple yes-or-no conditions inside a single integer. Each bit represents a separate property, option, permission, or state.
Key concepts
Bitwise logic; Bit positions; Bitmask representation; Boolean states; Permission flags; Bit shifting
Time complexity
operation: Check one bit; complexity: O(1) for fixed-width integers; operation: Set, clear, or toggle one bit; complexity: O(1) for fixed-width integers; operation: Compare or combine two masks; complexity: O(1) for fixed-width integers
Space complexity
O(1) for a fixed number of Boolean states stored in one integer.
Example
scenario: A software feature needs to use bitwise operators and bitmasking.; task: Design a small, testable solution that demonstrates bitwise operators and bitmasking while satisfying the requirements described in this entry.; approach: Identify the input and expected output, apply the central idea of bitwise operators and bitmasking, and trace the result with a small representative dataset.; learningPoint: This bitwise operators and bitmasking example should be checked with ordinary inputs, boundary conditions, and a case that could expose an incorrect assumption.
Applications
User permissions and access-control flags; Feature configuration; Game character abilities and inventory states; Subset representation; Operating-system flags; Competitive programming
Advantages
Stores multiple Boolean values in a compact representation.; Supports fast state checks and updates.; Makes it easy to combine and compare collections of flags.; Is useful for subset representation in algorithms.; Can reduce memory overhead compared with separate Boolean variables.