1. Home
  2. Developer tools
  3. Bitmask Calculator

Bitmask Calculator

Store several yes/no options in one integer, and decode the mysterious number in a database column back into names.

Free, no sign-up. Runs in your browser: nothing is uploaded.

How to use Bitmask Calculator

  1. Enter flag names, one per line.
  2. Tick flags or type a value to decode it.
  3. Copy the expression or the generated enum.

Features

  • Decimal, hex and grouped binary views
  • Decode any value back to flag names
  • Warning when bits above the named flags are set
  • Enums: TypeScript, C# [Flags], Python IntFlag, Rust bitflags, Go iota, C
  • Up to 32 unsigned flags

Supported formats

Input

  • Form input

You get

  • TS
  • CS
  • PY
  • RS
  • GO
  • H

Private by design

Everything you enter is processed by your browser on this device. Nothing is sent to openWorkstand or anyone else, so the Bitmask Calculator is safe for private and work data. After you have used it once while online, it also works without an internet connection. How local processing works

Good to know

  • Limited to 32 unsigned bits.

Guide

Bit flags and bitmasks: combining options in one number

Many systems store several yes-or-no options in a single integer: file permissions, feature toggles, user roles, hardware registers and protocol headers all do it. Each option occupies one bit, so one number can hold up to 32 or 64 independent switches and be checked with a single, very fast operation. This calculator names the bits, combines and decodes values and writes the matching enum for your language.

Why powers of two

Each flag is given a value with exactly one bit set: 1, 2, 4, 8, 16 and so on, written in code as 1 << 0, 1 << 1, 1 << 2. Because no two flags share a bit, any combination adds up to a unique number, and the number can always be taken apart again. Read, Write and Share as 1, 2 and 8 combine to 11, and 11 can only mean those three.

The four operations

Combining flags uses bitwise OR: Read | Write sets both bits. Testing uses AND: (value & Write) != 0 is true when the Write bit is set. Removing a flag uses AND with the inverted flag: value & ~Write clears that bit and leaves the others. Toggling uses XOR: value ^ Write flips it. These operations take a single CPU instruction, which is why flags are popular in performance-sensitive code.

Three views of one value

The decimal value is what usually ends up in a database column or a configuration file. The hexadecimal value lines up with bits in groups of four, so 0xB is the same as binary 1011. The binary view, grouped in fours from the right, shows each flag’s bit directly. You can tick flags, or type a decimal or hex value to decode it, and the other views update.

Decoding stored values

Paste the mysterious number from a database or log and read which named flags it contains. If the value has bits set above the last flag you named, the calculator warns you, which usually means the list of flag names is incomplete or the data came from a newer version of the software.

The generated enums

TypeScript gets an enum with None = 0, each flag as 1 << n and an All member combining every flag. C# gets the [Flags] attribute, which also makes ToString() print the names of combined values. Python gets an IntFlag class with auto() values, Rust a bitflags! block for the bitflags crate, Go a typed constant block using 1 << iota, and C a typedef enum with unsigned shifts.

A worked example

The Sample defines five permission flags: Read, Write, Delete, Share and Admin, as 1, 2, 4, 8 and 16, and sets the value 11. The calculator shows the binary 0 1011, the flags set as Read, Write and Share, and the expression Permission.Read | Permission.Write | Permission.Share. The TypeScript enum contains Share = 1 << 3, and All = Read | Write | Delete | Share | Admin,. Switching to Go writes the same flags as constants starting with Read Permission = 1 << iota.

Pitfalls

Languages differ in integer size and signedness, so the 32nd flag can turn values negative in JavaScript and Java; use unsigned types or 64-bit integers where available. Never reorder or remove flags once values are stored, and document the meaning of each bit next to the enum. For more than a few dozen options, a set of named columns or a list may be clearer than a mask.

Checked against the current version of the Bitmask Calculator on .

Frequently asked questions

Why does JavaScript give a negative number for 1 << 31?

JavaScript bitwise operators work on signed 32-bit integers, so the top bit is the sign. 1 << 31 is -2147483648. Append >>> 0 to read the result as unsigned, which is what this calculator does to show values up to 4,294,967,295.

Can I remove a flag from the middle of the list later?

Not without breaking stored values, because every later flag would shift to a new bit. Leave the old bit unused, keep its name with a note such as Deprecated, and add new flags at the end.

How do I check whether a flag is set?

AND the value with the flag and compare with zero: (value & Write) != 0. To add a flag use value | Write, to remove it value & ~Write, and to toggle it value ^ Write.

Why start the enum with None = 0?

Zero means “no flags”, and C# guidelines for [Flags] enums recommend naming it. Without a named zero, an empty value prints as the number 0 instead of a readable name.

Keep working with the result

openWorkstand also includes a free workspace with documents, spreadsheets, tasks, projects and a calendar that save to a folder on your computer.