Bitwise Operator Calculator

JJ Ben-Joseph headshot JJ Ben-Joseph

Enter numbers and choose an operation.

Binary Thinking and Bitwise Manipulation

Digital computers store information as sequences of binary digits, or bits, which take on the value 0 or 1. Each bit corresponds to a power of two. For example, the eight-bit pattern 01000010 represents the decimal value 66 because 2^6=64 and 2^1=2; summing these yields 66. Bitwise operations act directly on these representations, enabling low-level manipulations that are extremely efficient.

Consider two unsigned integers A and B. Many programming languages expose operators that work on their bit patterns. This page lets you explore six fundamental operations: AND, OR, XOR, NOT, left shift, and right shift. The first three combine corresponding bits of two numbers to yield a result, while NOT inverts each bit of a single number. Shifts move bits left or right, effectively multiplying or dividing by powers of two. Understanding these operations is essential for tasks like setting flags, packing data, crafting cryptographic primitives, or optimizing arithmetic.

Truth Tables

At the heart of bitwise logic lies the truth table. For AND, a bit in the result is 1 only when the corresponding bits of both inputs are 1. OR yields 1 if either input has a 1. XOR, short for “exclusive OR,” produces 1 when exactly one input bit is 1. The following table summarizes these behaviours:

ABA AND BA OR BA XOR B
00000
01011
10011
11110

NOT, denoted by a tilde in languages like C (~A), simply flips each bit: 0 becomes 1 and 1 becomes 0. When numbers use two’s complement representation, NOT of A equals -A-1.

Shifting

Left shift moves bits toward the most significant end, inserting zeros on the right. Shifting A left by n places multiplies it by 2n as long as no significant bits overflow the word size. Right shift has two flavors. Logical right shift introduces zeros on the left, effectively dividing by 2n. Arithmetic right shift preserves the sign bit when handling signed integers, ensuring that negative numbers remain negative. The calculator presented here uses JavaScript’s unsigned right shift to maintain predictable results.

Binary, Decimal, and Hexadecimal

Because humans find long binary strings cumbersome, programmers often use decimal or hexadecimal notation. Hexadecimal digits base sixteen range from 0 to 15, commonly written as 09 and AF. Each hex digit corresponds to four bits, simplifying conversions. For example, 0x3F equals binary 00111111. This page accepts input in decimal, binary, or hexadecimal. Internally, each string is converted to a JavaScript BigInt with parseInt(), then operations are performed using BigInt operators.

Mathematical Formulation

Let ai and bi denote the i-th bits of two numbers. Bitwise AND can be expressed as ci=ai×bi, treating bits as elements of {0,1}. XOR corresponds to addition modulo two: ci=(ai+bi)mod2. Left shift by n multiplies by 2n, i.e., A<<n=A×2n.

Applications

Bitwise operations appear throughout computing. In graphics programming, individual color channels are packed into a single integer; masking with AND and shifting extracts each channel. Network protocols define bit fields to represent flags like SYN or ACK in TCP headers. File permissions in Unix systems rely on bit patterns such as rwxr-xr-x; a bitwise OR combines permissions, and AND verifies them. Cryptographic algorithms often mix XOR, shifts, and additions to achieve diffusion and confusion. For example, the classic Feistel structure uses XOR to mix subkeys with data, while modern stream ciphers combine shift registers with XOR gates.

Working With Signed Values

Most programming languages encode signed integers using two’s complement. In this scheme the most significant bit denotes sign, and negative numbers are obtained by inverting all bits of the positive value and adding one. When applying bitwise operations to signed values, it is important to consider how the sign bit behaves. Left shifting a signed integer may overflow into the sign bit, flipping the sign unexpectedly. Logical right shift fills vacated bits with zeros, potentially changing a negative number to positive. Arithmetic right shift replicates the sign bit, keeping negative numbers negative. JavaScript’s bitwise operators implicitly convert numbers to 32-bit signed integers. To avoid truncation, this calculator uses the BigInt-based versions &, |, ^, ~, <<, and >>, maintaining full precision.

Example

Suppose A=13 and B=6, entered in decimal. In binary these correspond to 1101 and 0110. The AND result is 0100 (4), OR yields 1111 (15), and XOR gives 1011 (11). NOT of 13 assumes an infinite leading zero representation and flips all bits, effectively computing -14. Left shifting 13 by two positions multiplies it by 4, producing 52. Right shifting by one divides by 2, giving 6.

Further Reading

The ability to reason about bits permeates computer science. Boolean algebra, developed in the nineteenth century by George Boole, provides a rigorous foundation for the logical operations implemented by modern hardware. Claude Shannon recognized that electrical circuits could represent Boolean expressions, launching the field of digital electronics. Microprocessors integrate billions of transistors implementing these operations at staggering speed. Algorithms from compression to cryptography leverage bitwise manipulations to achieve efficiency and security. Mastering these concepts equips programmers to optimize performance, understand data formats, and debug low-level issues.

Below is a summary table showing how different operations transform the example numbers 5 (0101) and 3 (0011).

OperationResult (Decimal)Result (Binary)
AND10001
OR70111
XOR60110
NOT (5)-6...11111010
5 << 34000101000
5 >> 120010

This calculator operates entirely in your browser. You can save the HTML file and run it offline to experiment with bitwise operations, making it a handy reference for students, hobbyists, and professionals alike. By observing how numbers change under each operation, you build intuition that carries over to all programming languages. Whether you are configuring network masks, crafting cryptographic puzzles, or optimizing shaders, a solid grasp of bit-level manipulation will serve you well.

Related Calculators

Shift Differential Pay Calculator - Estimate Earnings

Calculate total pay including shift differential for evening, night, or weekend work.

shift differential pay calculator night shift wage

Sagnac Interferometer Phase Shift Calculator

Compute the phase shift and time delay observed in a rotating Sagnac interferometer from its geometry, rotation rate, and light wavelength.

Sagnac effect calculator ring laser gyroscope phase shift

Raman Shift Calculator - Stokes and Anti-Stokes Wavelengths

Convert Raman shift values to scattered wavelengths for spectroscopy experiments.

Raman shift calculator Stokes wavelength spectroscopy