Home › Bitwise Calculator

Bitwise Calculator

Developer toolRuns in your browserNothing uploaded

Bitwise operations

The operations

Bitwise operators work on the individual bits of a number rather than its value.

Worked example: 12 is 1100 and 10 is 1010. AND gives 1000 = 8. OR gives 1110 = 14. XOR gives 0110 = 6.

Bit flags, the most common practical use

Storing many boolean values in a single integer is efficient and appears throughout systems programming, file formats, and permission systems.

Each flag is assigned a distinct power of two: READ = 1, WRITE = 2, EXECUTE = 4, DELETE = 8. A permission set is the OR of the flags it includes, so read plus write is 1 | 2 = 3.

Unix file permissions work exactly this way — which is why chmod 755 makes sense once you see 7 as 111 and 5 as 101.

XOR and its useful properties

XOR has two properties that make it unusually versatile.

It is its own inverse. (a ^ b) ^ b == a. This underlies the one-time pad, the strongest possible cipher when the key is truly random, as long as the message, and never reused. It also means XOR alone is trivially breakable when the key repeats.

A value XORed with itself is zero. This gives an elegant solution to a classic problem: given an array where every value appears twice except one, XORing everything together leaves the unpaired value, in O(n) time and O(1) space.

XOR also appears in RAID parity, checksums, hash mixing, and simple graphics operations where drawing twice restores the original.

Shifts and the pitfalls

Shifting is often faster than multiplication or division by powers of two, though modern compilers make this optimisation automatically — writing x >> 3 instead of x / 8 for speed is rarely worthwhile and usually less readable.

Two behaviours catch people out:

Right shift on negative numbers. Arithmetic right shift (>>) preserves the sign bit, so -8 >> 1 is −4. Logical right shift (>>> in JavaScript) fills with zeros, turning a negative into a large positive.

JavaScript truncates to 32 bits. All bitwise operators convert their operands to signed 32-bit integers, so values above 2³¹−1 behave unexpectedly. BigInt supports bitwise operations for larger values.

Where you will encounter this

Networking. Subnet masks are applied with AND to extract the network portion of an address.

Graphics. Packing RGBA into a single 32-bit integer and extracting channels with shifts and masks.

Compression and encoding. Bit-level packing where every bit counts.

Hashing. Most hash functions mix bits with shifts and XOR.

Embedded systems. Hardware registers are manipulated bit by bit, and this is often the only way to configure a peripheral.

In ordinary application code, bit manipulation is worth reaching for when working with flags, protocols, or performance-critical inner loops — and worth avoiding when a boolean field would be clearer.

Frequently asked questions

What is the difference between & and &&?

The single ampersand is a bitwise AND that operates on individual bits of numbers. The double is a logical AND that operates on true and false values and short-circuits.

Why is ~5 equal to -6?

Because NOT inverts every bit of a signed integer, including the sign bit. In two's complement representation, the inverse of n is always -(n+1).

How do bit flags work?

Each flag is a distinct power of two, so they occupy separate bits. Combine with OR, test with AND, clear with AND NOT, and toggle with XOR. Unix file permissions use exactly this scheme.

Why do JavaScript bitwise operations behave oddly on large numbers?

All bitwise operators convert operands to signed 32-bit integers, so values above 2^31-1 wrap unexpectedly. Use BigInt for larger values.