Bit Set, Clear & Toggle Calculator
Set, clear, toggle, or test bit k of an integer and see the result in decimal, binary, and hex.
Action
bits
Bit at position 3 = 1
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
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
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
TG we-Calculate Editorial Team. "Bit Set, Clear & Toggle Calculator." TG we-Calculate. 2026. https://we-calculate.com/calculator/bit-set-clear-toggle-calculator.
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
@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?
