Bitwise & Register Field Calculator

Convert between hex, decimal and binary, flip bits, and read or write register fields, with ready-to-paste C code.

Spaces, underscores and a 0x or 0b prefix are fine. A minus sign works in decimal.
Hexadecimal
Decimal (unsigned)
Decimal (signed, two’s complement)
Binary

Bits (click a bit to flip it)

Register field

Writes this into the field and shows the new register.
Field mask
Field value now
Register after writing the new value

C code

Operation

Result (hex)
Result (decimal)
Result (binary)

How it works

A register is just a row of bits. The calculator keeps the value as an exact integer of the chosen width (8, 16, 32 or 64 bits), shows it in every base, and lets you flip individual bits. A field is a group of neighboring bits with a meaning, such as a prescaler or a mode selector.

Mask for a field from bit L to bit H: mask = (2(H − L + 1) − 1) × 2L
Read a field: field = (reg ÷ 2L) mod 2(H − L + 1) (in C: (reg >> L) & mask)
Write a field: reg = (reg with the field cleared) + new × 2L (in C: reg = (reg & ~(mask << L)) | ((val & mask) << L))
Signed value: if the top bit is set, value − 2width

Shifting left by n multiplies by 2n and drops the bits that fall off the top; shifting right divides by 2n and drops the low bits. A rotate moves the dropped bits around to the other end instead.

Worked example

Take a 32-bit register holding 0xDEADBEEF and a field in bits 15:8. The field has 8 bits, so its mask is (28 − 1) × 28 = 0xFF00. Reading it gives (0xDEADBEEF ÷ 28) mod 256 = 0xBE. Writing 0x42 into the field clears bits 15:8 and sets them to 0x42, so the register becomes 0xDEAD42EF. Separately, 0xDEADBEEF AND 0x0F0F0F0F = 0x0E0D0E0F, which keeps the low nibble of each byte.

Things to know

Read the datasheet’s bit numbering. Almost all MCUs number the lowest bit 0, but some documents list registers with bit 31 on the left and others with bit 0 on the left. Check the register diagram before you trust a mask.

Read-modify-write. To change one field without disturbing the rest, read the register, clear the field with a mask, OR in the new bits, and write it back. Some registers have separate set and clear registers or atomic bit-banding to avoid this.

Careful with write-one-to-clear flags. Writing back a value you just read can clear status flags by accident in registers where writing 1 clears a bit.

Signed versus unsigned. The same bits mean different numbers depending on the type. 0xFF is 255 as unsigned 8 bit and −1 as signed 8 bit; 0x80 is 128 or −128.

Use unsigned types and suffixes in C. For 64-bit masks, write 1ULL << n. Shifting a plain int by 31 or more is undefined behavior.

Endianness is separate. This tool shows the value as a number. How its bytes are stored in memory or sent over a bus depends on the processor and the protocol.

Common questions

How do I set, clear or toggle a single bit?

Set it with OR and a one-bit mask: reg |= (1u << n). Clear it with AND and the inverted mask: reg &= ~(1u << n). Toggle it with XOR: reg ^= (1u << n). The C code section of the calculator shows these for your bit.

Why did my value get cut to fewer bits?

The register width is fixed at 8, 16, 32 or 64 bits. A larger number does not fit, so only the low bits are kept, as in real hardware.

What is two’s complement?

The common way computers store negative integers. The top bit is the sign bit, and the value is the unsigned number minus 2width when that bit is set. Entering a negative decimal number converts it for you.

Can I enter a number with a 0x or 0b prefix?

Yes. A prefix overrides the base choice, so you can type 0x1F or 0b1010 whichever base is selected.

Sources

  • ISO/IEC 9899 (C language standard), sections on bitwise operators and shifts.
  • Arm, Cortex-M programmer manuals (bit-banding and read-modify-write).

Last reviewed: October 22, 2026