Free Bitwise Calculator
Perform bitwise operations on integers and view results in decimal, hex, and binary.
How It Works
- Enter two numbers: Input the values you want to operate on, in decimal, binary (prefix 0b), or hexadecimal (prefix 0x).
- Select the operation: Choose AND, OR, XOR, NOT, left shift (<<), or right shift (>>).
- View the result: The output shows the result in decimal, binary, and hex simultaneously, with a bit-level visualization.
Why Use Bitwise Calculator?
Bitwise operations are fundamental in systems programming, cryptography, game development, graphics, networking, and embedded systems. Understanding how AND, OR, XOR, and shift operations manipulate individual bits is critical for tasks like setting/clearing flags, packing data into compact formats, and implementing efficient algorithms. This calculator shows the operation at the bit level so you can see exactly how each bit is affected.
Features
- All bitwise operators: AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>).
- Multi-base input: Enter numbers in decimal, binary (0b...), or hexadecimal (0x...).
- Multi-base output: Results shown simultaneously in decimal, binary, and hexadecimal.
- Bit visualization: A visual bit grid shows which bits are set for each operand and the result.
- Signed/unsigned modes: Toggle between 8, 16, 32, and 64-bit integer widths.
Frequently Asked Questions
What is XOR used for in programming?
XOR (^) is used for toggling bits, simple encryption/obfuscation, swap operations without a temp variable, parity checks, and hash mixing. It returns 1 when bits differ and 0 when they are the same.
What is the difference between << and >>?
Left shift (<<) moves all bits to the left, equivalent to multiplying by powers of 2. Right shift (>>) moves bits right, equivalent to dividing by powers of 2. Arithmetic right shift preserves the sign bit; logical right shift fills with zeros.
How do I set or clear a specific bit?
To set bit n: value |= (1 << n). To clear bit n: value &= ~(1 << n). To toggle bit n: value ^= (1 << n). To check if bit n is set: (value & (1 << n)) !== 0.
From Boolean algebra to silicon: how bitwise ops became universal
The bitwise operations every modern CPU implements come from two foundational papers published more than 80 years apart. In 1854, George Boole published «An Investigation of the Laws of Thought», defining the algebra of two-valued logic, AND, OR, NOT, and their identities. It was a philosophical work, not an engineering one. Then in 1937, a 21-year-old MIT graduate student named Claude Shannon wrote his masters thesis «A Symbolic Analysis of Relay and Switching Circuits», proving that Boolean algebra could describe electrical relay circuits and therefore that any logical computation could be physically implemented. This thesis is widely cited as the most important masters thesis of the 20th century and is the foundation of all digital electronics. Two's complement, the binary representation for negative numbers that every CPU uses, was patented in 1962 by Burroughs Corporation but had been used in the IBM 704 (1954) and other early machines. IEEE 754 standardised floating-point representation in 1985, fixing the bit layout that every JavaScript Number still uses, 1 sign bit, 11 exponent, 52 mantissa for binary64. Today, the C language operators & | ^ ~ << >> map almost directly to single CPU instructions, which is why bitwise code can be dramatically faster than the arithmetic equivalent.
The six operators, what they do at the bit level
- AND (
&). Result bit is 1 only if both input bits are 1.0b1100 & 0b1010 = 0b1000. Use to mask, isolate specific bits, or check whether a flag is set:flags & FLAG_READis non-zero iffFLAG_READis on. - OR (
|). Result bit is 1 if either input bit is 1.0b1100 | 0b1010 = 0b1110. Use to set bits, combine flags:flags = FLAG_READ | FLAG_WRITE. - XOR (
^). Result bit is 1 if exactly one input bit is 1.0b1100 ^ 0b1010 = 0b0110. Use to toggle bits, simple obfuscation (x ^ x = 0, so XOR is its own inverse), parity, and hash mixing. - NOT (
~). Flip every bit.~0b0000_1111 = 0b1111_0000(in 8 bits). In two's complement,~xequals-x − 1, so~5 === -6in JavaScript. - Left shift (
<<). Move all bits left by n positions, fill the right with zeros.1 << 3 === 8, equivalent to multiplying by 2³. Useful for building bit masks:1 << nis «the bit at position n». - Right shift (
>>arithmetic,>>>logical). Move bits right. Arithmetic shift (>>in JS, Java, C signed) preserves the sign bit; logical shift (>>>in JS,>>in C unsigned) fills with zeros.-8 >> 1 === -4but-8 >>> 1 === 2147483644in JavaScript because>>>coerces to a 32-bit unsigned int first.
Where bitwise actually earns its keep
- Bit flags and feature toggles. Pack 32 boolean options into a single integer. Linux file permissions (
chmod 755), network protocol headers (TCP, IPv4), and OpenGL state flags all use this pattern. One&check tells you whether a feature is enabled. - Unix file permissions.
rwxr-xr-xis0b111_101_101 = 0o755 = 493. The three groups are user/group/other, each three bits are read/write/execute. The chmod calculator turns the symbolic form into the integer your shell wants. - IP subnet masks. A
/24mask is255.255.255.0or0xFFFFFF00. To check if an IP is in a subnet:(ip & mask) === (network & mask). Used by every router and firewall in the world. - Cryptography and hashing. SHA-256, AES, ChaCha20, BLAKE3, every modern hash and cipher is built primarily from AND, OR, XOR, NOT, and rotates. XOR alone is the basis of one-time pads, which are provably unbreakable when the key is truly random and used once.
- Color manipulation. A 32-bit RGBA color packs four 8-bit channels:
(R << 24) | (G << 16) | (B << 8) | A. To extract the red channel:(color >> 24) & 0xFF. Used by HTML canvas, OpenGL, every image format. - Performance tricks.
x & 1is much faster thanx % 2on most CPUs (one cycle vs ~10).x >> 1divides by 2 in one cycle. Power-of-two alignment, bit-population counts, and bit-twiddling hacks are everywhere in performance-critical code. (See Sean Anderson's «Bit Twiddling Hacks» page for a famous catalogue.) - Embedded systems. Microcontrollers map hardware registers to specific bit positions in memory. Configuring a UART or GPIO pin requires masking and shifting to read or write the right bits without disturbing the rest. C's bitfield syntax is convenient sugar for these patterns.
Mistakes that bite
- Confusing
&with&&and|with||. The single forms are bitwise, the doubles are short-circuit logical.1 & 2 === 0(no shared bits) but1 && 2 === 2(both truthy, returns the second). Mixing them up silently breaks conditional logic. - Operator precedence surprises.
x & FLAG === 0meansx & (FLAG === 0), not(x & FLAG) === 0, because===binds tighter than&. Always parenthesise bitwise expressions in conditions. - Signed-vs-unsigned shift confusion. In JavaScript,
>>is arithmetic (sign-extending),>>>is logical (zero-filling). C distinguishes by the variable type, signed types use arithmetic, unsigned use logical. Java has both>>and>>>like JavaScript. Mismatching the language convention silently flips the meaning. - JavaScript truncates to 32 bits for bitwise ops. Despite
Numberbeing a 64-bit float, all bitwise operators coerce operands to int32 first.0x100000000 & 0xFF === 0, not 0 + the low byte, because the operand is truncated to 32 bits before the AND. For 64-bit bit manipulation useBigInt, which has its own bitwise operators. - Shifting by > 31 in JavaScript.
1 << 32 === 1, not 0. The shift amount is taken modulo 32. C is even more dangerous: shifting by more than the bit width is undefined behaviour, so compilers can do anything. Always checkn < bitWidthbefore shifting by a runtime value. - Endianness in byte-packing code.
(r << 24) | (g << 16) | (b << 8) | aproduces0xRRGGBBAA. Whether bytes are stored in that order or reversed in memory depends on the platform. ImageData in HTML canvas is little-endian on x86; PNG file format is big-endian. Convert explicitly withDataViewwhen reading binary formats. - Forgetting two's complement.
~0 === -1, not0xFFFFFFFF, because all-ones is the two's complement encoding of −1. To get the unsigned 32-bit interpretation in JavaScript:(~0) >>> 0 === 4294967295.
Why two's complement, and what it means at the bit level
Every modern CPU represents negative integers using two's complement. In a signed 8-bit byte, the values 0 to 127 are encoded as binary 0000_0000 to 0111_1111. Then −1 is encoded as 1111_1111, −2 as 1111_1110, down to −128 as 1000_0000. The reason: with this encoding, addition works the same way whether the inputs are signed or unsigned, the CPU does not need separate add-signed and add-unsigned instructions. The asymmetry is that the negative range is one larger than the positive (in 8 bits, −128 to +127), which is why Math.abs(INT_MIN) overflows in every language with fixed-width integers. The earlier sign-magnitude (one bit for sign, rest for magnitude) and one's-complement encodings existed in the 1950s-60s but lost out to two's complement because they had two representations of zero and required special-case hardware for negation.
More frequently asked questions
Why is ~5 equal to -6 instead of 250?
Because in two's complement (the encoding every modern CPU uses), flipping every bit of a positive number gives you -n - 1. So ~5 === -6 and ~0 === -1. In an unsigned 8-bit context, the same bit pattern as ~5 (binary 1111_1010) would represent 250. JavaScript treats the result as signed 32-bit, so you see -6. To get the unsigned interpretation: (~5) >>> 0 in 32-bit, which gives 4294967290, or mask to 8 bits with ~5 & 0xFF which gives 250.
Is XOR really an encryption?
XOR is the building block of every modern symmetric cipher, but XOR alone is not secure encryption. A one-time pad, XOR with a truly random key as long as the message, used exactly once, is information-theoretically unbreakable (Shannon, 1949). Reuse the key, or use a key shorter than the message, and frequency analysis breaks it trivially. Real ciphers like AES use XOR plus diffusion and substitution to amplify a short key into a stream of pseudo-random bytes that look like a one-time pad to anyone without the key. So «encrypt with XOR» is fine only for trivial obfuscation, never for real secrets.
When should I use BigInt instead of Number for bit manipulation?
Any time you need more than 32 bits of bitwise precision. JavaScript bitwise operators truncate Number operands to 32-bit signed integers before computing. If you need 64-bit masks (e.g., manipulating a 64-bit feature flag set, working with Linux mmap offsets, or implementing SHA-512), use BigInt: 0xFFFFFFFFFFFFFFFFn & 0xFFn === 255n. BigInt is slower than Number, ~3-10× depending on operation, so reserve it for the cases where 32 bits is genuinely too few.
How do I count the number of 1 bits in a number?
This is the population count or Hamming weight. Most modern CPUs have a single instruction for it (POPCNT on x86, VCNT on ARM). In JavaScript, no built-in, so use the bit-twiddling classic: let c = 0; while (x) { c += x & 1; x >>>= 1; }. Or the parallel SWAR trick from Hacker's Delight: x = x - ((x >> 1) & 0x55555555); x = (x & 0x33333333) + ((x >> 2) & 0x33333333); x = (x + (x >> 4)) & 0x0F0F0F0F; return (x * 0x01010101) >>> 24;, which counts the bits in a 32-bit integer in roughly a dozen cycles.
Is my data sent anywhere when I use this calculator?
No. Every operation runs in your browser's JavaScript engine, no network calls happen during a calculation. Open the Network tab in DevTools and click Calculate, you will see zero outbound requests. Safe for sensitive masks, keys, or proprietary bit-layout work.