Skip to content
ToolBoxGeniehome

Binary Calculator

Developer Tools · Added

Two values, one operation, and a word size that actually matters. This computes arithmetic and every bitwise operation on fixed-width integers from 8 to 64 bits, shows both operands and the result as bit patterns, and reports overflow rather than hiding it — so when 200 plus 100 comes out as 44 in a byte, you can see exactly why. It is built on arbitrary-precision integers underneath, so 64-bit values behave correctly rather than being silently truncated to 32.

Results wrap to this width

Try:

How to use the binary calculator

  1. 1Choose the base your values are written in — binary, octal, decimal or hexadecimal.
  2. 2Pick the word size. Results wrap to this width, which is what a real processor does.
  3. 3Choose the operation: arithmetic, a bitwise logic operation, or a shift.
  4. 4Enter the two values; for a shift, the second box is the number of places.
  5. 5Turn on signed mode to read the values as two's complement, where the top bit means negative.

Examples

Masking with AND

Input
1010 & 0110 in binary
Result
0010 — only the bit set in both survives

The most common use of AND is testing or clearing specific bits with a mask.

A shift as a multiplication

Input
1 << 4 in decimal
Result
16

Each place shifted left doubles the value, so shifting by four multiplies by sixteen.

Overflow in a byte

Input
200 + 100, 8-bit unsigned
Result
44 — with the true result of 300 reported alongside

The high bit is discarded because it does not fit. This is where a whole category of security bugs comes from.

About the binary calculator

What the bitwise operations are actually for

AND with a mask tests or clears bits. `flags & 0x04` is non-zero exactly when the third bit is set, which is how a status register is read; `flags & ~0x04` clears that bit and leaves the rest alone. OR sets bits: `flags | 0x04` turns the third bit on regardless of what it was. Together they are how a single integer stores a dozen independent yes-or-no settings, which is why they turn up in every file permission, every graphics API and every network protocol header.

XOR is the interesting one. It flips exactly the bits set in the mask, and applying the same XOR twice returns the original — which makes it the basis of the simplest possible cipher, a fast way to swap two variables without a temporary, and the mechanism behind parity and checksum calculations. It also detects difference: `a ^ b` is zero exactly when the two are equal.

Shifts are multiplication and division by powers of two, and historically that was their point — a shift is a single fast instruction where a multiply was many. Compilers now do that substitution themselves, so writing `x << 3` instead of `x * 8` no longer buys speed and does cost readability. Where shifts remain the clear expression is in packing and unpacking fields: extracting the red channel from a 24-bit colour is `(rgb >> 16) & 0xFF`, and no arithmetic phrasing is clearer.

Overflow, and why it is worth seeing

When a result does not fit the word, the bits above the top are discarded and what remains is the answer modulo the word size. For unsigned arithmetic that wraps to zero; for signed arithmetic it wraps from the largest positive value to the most negative, which is the more surprising case — adding 1 to a 32-bit signed 2147483647 gives −2147483648.

This is not a rare edge case, it is a major category of software defect. A length check that adds two sizes and compares the result can be defeated by making the sum overflow to something small. A counter that wraps can cause a loop to run for far longer than intended, or not at all. The Ariane 5 launch failure in 1996 came from a 64-bit float being converted to a 16-bit signed integer that could not hold it.

The reason this page reports the true result alongside the wrapped one is that seeing both is what makes the behaviour intelligible. A calculator that quietly returns 44 for 200 plus 100 is technically correct for an 8-bit word and teaches nothing; showing that the real answer was 300 and that 256 of it was discarded shows exactly where the missing value went.

Frequently asked questions

Why does the word size change the answer?
Because a fixed-width integer has nowhere to put bits that do not fit, and the ones above the top are simply discarded. In an 8-bit word the largest unsigned value is 255, so 200 plus 100 wraps around to 44. In a 16-bit word the same sum is 300. Neither is wrong — they are answers to different questions, and choosing the width is how you say which processor or which variable type you mean.
What is two's complement, and why is 11111111 equal to −1?
It is the convention almost every processor uses for signed integers: the top bit indicates a negative value, and a negative number is stored as the bitwise complement of its magnitude plus one. It is chosen because it makes subtraction the same circuit as addition — adding 1 to 11111111 overflows to 00000000, which is exactly what −1 plus 1 should give. The bits do not change when you switch this page between signed and unsigned; only the agreement about how to read them does.
What is the difference between the two right shifts?
A logical shift feeds zeros in at the top, so the result is always positive whatever the original sign bit was. An arithmetic shift copies the sign bit inward, so a negative number stays negative — which is what makes it a division by a power of two for signed values. Both exist in every real instruction set, and languages differ in which one their `>>` operator means, so it is worth being explicit.
Why not just use JavaScript's bitwise operators?
Because they coerce both operands to 32-bit signed integers before doing anything. That means `0xFFFFFFFF | 0` is −1 rather than 4294967295, and `1 << 32` is 1 rather than 4294967296. Any calculator built on them gives wrong answers on exactly the large values somebody would use it to check. This page works in arbitrary-precision integers and masks explicitly to the width you chose, which is why 64-bit results are correct.
Why is shifting by more than the word size refused?
Because there is no agreed answer. In C it is undefined behaviour, and real processors differ: x86 masks the shift count to the low five or six bits, so shifting a 32-bit value by 32 shifts it by zero and returns it unchanged, while ARM produces zero. Returning either would teach you something that is only true on some hardware, so this refuses and says why.