Other

Flag (Bitmask) Calculator

Combine boolean flags into one value, or decode a value back into its flags.


Flag (Bitmask) Calculator

Advertisement

A common programming utility for combining several boolean “flags” (each a power of two) into a single integer, and reading them back out.

How it works

Combining flags uses a bitwise OR across the selected power-of-two values; decoding checks which power-of-two values are present in the entered combined number using a bitwise AND.

What this does not include

This does not include non-power-of-two flag systems — standard bitmasks require each flag to be a distinct power of two so they never overlap in bits.

How to use this calculator

  1. Choose a direction, then enter either the flags to combine or a value to decode.

Frequently asked questions

Why must each flag be a power of two?

Because powers of two each occupy a single, distinct bit position in binary — combining them with addition or OR never causes two different flags to collide into an ambiguous combined value.

Where is this pattern commonly used?

Permission systems (read/write/execute), CSS or configuration option flags, and low-level API settings that pack multiple boolean options into one integer.

What happens if I enter a non-power-of-two flag?

It’s still added, but may not decode cleanly back into a single distinct flag if it overlaps in bits with another value already in the set.

Be the first to rate this

Written by

S. Tanaka

Everyday-life writer

S. Tanaka writes the everyday-life calculators: time and date arithmetic, reading speed, tips, and the small practical sums people look up mid-task. The aim is a page that answers the question in the first screen and explains the method underneath for anyone who wants it. Simple topics still deserve a stated method.

Reviewed by

P. Raman

Calculator reviewer — general reference

P. Raman reviews calculators across the general reference categories, checking that each formula matches the definition the page claims for it and that the worked test values actually reproduce. The review also covers scope: whether a page is answering a subtly different question from the one its title promises, and whether it overlaps a calculator that already exists.

How we write and review

Related calculators