Intermediate

Bit Set, Clear & Toggle Calculator

Set, clear, toggle, or test bit k of an integer and see the result in decimal, binary, and hex.
Non-negative integer
0 = least significant bit

Action

bits

1 to 64
New value (decimal)
42

Bit at position 3 = 1

Binary
00101010
Hexadecimal
0x2A
Original binary
00101010
Queried bit value
1
031.963.895.6127.5159.4191.3223.1255Original and new value in the 8-bit range
Results are estimates for general information only and are not professional advice — always verify important results independently before relying on them. Read the full disclaimer.
Quick answer

How does this calculator work?

To change bit k of an integer, build mask = 1 << k. Set with n | mask, clear with n & ~mask, toggle with n ^ mask, and read a bit with (n >> k) & 1. This tool applies the operation, masks the result to your chosen bit width, and shows it in decimal, binary, and hexadecimal.

Formula
mask = 1 << k | Set: n | mask | Clear: n & ~mask | Toggle: n ^ mask | Test: (n >> k) & 1
How this is calculated

Enter a non-negative integer n, a zero-based bit position k (where 0 is the least significant bit), an action, and a bit width w (1-64). A mask is built as mask = 1 << k, isolating the single target bit. The integer is first reduced modulo the chosen width using widthMask = (1 << w) - 1 so only the lowest w bits are considered.

Each action applies a standard bitwise operation. Set uses n | mask to force the bit to 1. Clear uses n & ~mask to force it to 0, where ~mask is taken within the width so higher bits are not affected. Toggle uses n ^ mask to flip the bit. Test leaves the value unchanged and simply reports the queried bit via (n >> k) & 1. After the operation the result is masked again with widthMask to keep it inside w bits.

The result is shown in decimal, as a w-bit zero-padded binary string, and in hexadecimal, alongside the value of the queried bit. All arithmetic uses arbitrary-precision integers (BigInt) so widths up to 64 bits are exact. Inputs must be integers; k must satisfy 0 <= k < w, and n must be non-negative.

Frequently asked questions

Yes. Position 0 is the least significant (rightmost) bit, position 1 is next, and so on, up to width - 1.

Test reads the bit without changing the value. It returns the current value of bit k as either 0 or 1 while leaving n unchanged.

The width mask (1 << w) - 1 keeps the result within w bits, matching fixed-width registers and ensuring clear and toggle do not flip bits outside the chosen size.

Also known as

bit manipulation calculator
set clear toggle bit
bitmask calculator
test bit
bit flag calculator
flip bit
bit set clear

APA

TG we-Calculate Editorial Team. (2026). Bit Set, Clear & Toggle Calculator [Online calculator]. TG we-Calculate. https://we-calculate.com/calculator/bit-set-clear-toggle-calculator

Chicago

TG we-Calculate Editorial Team. "Bit Set, Clear & Toggle Calculator." TG we-Calculate. 2026. https://we-calculate.com/calculator/bit-set-clear-toggle-calculator.

IEEE

TG we-Calculate Editorial Team, "Bit Set, Clear & Toggle Calculator," TG we-Calculate, 2026. [Online]. Available: https://we-calculate.com/calculator/bit-set-clear-toggle-calculator

BibTeX

@misc{wecalculate_bit_set_clear_toggle_calculator, title = {Bit Set, Clear & Toggle Calculator}, author = {{TG we-Calculate Editorial Team}}, howpublished = {\url{https://we-calculate.com/calculator/bit-set-clear-toggle-calculator}}, year = {2026}, note = {TG we-Calculate} }

Did this calculator help you?