003-005-004

Bitwise Operations: Basics of Flag Management

Medium

Problem Description

In this problem, you will create a program that manages multiple ON/OFF flags using an integer variable with bitwise operations (OR, AND, shift) to set, clear, and check flags, then displays the results to standard output.

Flag Management with Bitwise Operations

Bitwise operations efficiently manage multiple ON/OFF flags in a single integer.

Basic Operations

// Set bit (make it 1)
flags |= (1 << position);

// Clear bit (make it 0)
flags &= ~(1 << position);

// Check bit
boolean isSet = (flags & (1 << position)) != 0;

Learning Points

  • | (OR): Set bits
  • & (AND): Check bits, combine with ~ to clear
  • << (left shift): Move 1 to specified position
  • Used in practice for permission management etc.

Ready to Try Running Code?

Log in to access the code editor and execute your solutions for this problem.

Don't have an account?

Sign Up