Bitwise calculator
AND, OR, XOR, NOT and both kinds of shift, with the bits laid out so you can see what happened. It works above 32 bits, which is where nearly every other one on the web silently stops being correct. Nothing you type here leaves your browser — it is worked out on your own device, so there is nothing to send and nothing to log.
Why a calculator like this is usually wrong
JavaScript's bitwise operators do not work on numbers. They convert their operands to 32-bit signed integers first, do the operation on those, and hand back a signed 32-bit result. Everything above that is discarded without a word:
| 240 ¦ 0 | gives 0, not 1,099,511,627,776 |
|---|---|
| 4294967295 ¦ 0 | gives −1, because the 32nd bit is read as a sign |
| −8 >>> 1 | gives 2,147,483,644, while −8 >> 1 gives −4 |
So a bitwise calculator built the obvious way is wrong for every input
above about four billion. Not slow, not approximate — wrong, and
returning a number rather than an error. This page uses
BigInt, which is arbitrary precision and has none of those
limits, and it shows you what the 32-bit answer would have been so you
can see the difference.
And the obvious test would not have found it. 123456789012 AND 255 gives 20 in JavaScript and 20 in Python, because masking down to a low byte is precisely the case truncation cannot damage. The value anybody would reach for to check a bitwise function is the one value that cannot tell a correct implementation from a broken one. The checks behind this page are swept across every base and four bit widths against Python, whose integers have no size limit at all.
One more thing, and it is not about bitwise. A
JavaScript number stops being able to hold consecutive integers above
253. Type 9007199254740993 into most calculators and it
becomes 9007199254740992 — the literal is wrong before anything is
done to it. This page reads your digits one at a time into a
BigInt, so what you typed is what it works with.
NOT is the one that needs a width
AND, OR and XOR do not care how wide the number is: the answer is the same whether you think of 12 as eight bits or sixty-four. NOT is different. Not 5 has no answer until you say how many bits you are flipping — 250 in a byte, 65,530 in sixteen bits, and a different number again in thirty-two.
JavaScript's BigInt answers ~5n with
-6n, because it treats the value as an infinitely long
two's complement number. That is a consistent answer and almost never
the one you wanted. This page brings the result back to the width you
chose and says which width it used.