Bitwise Operations Calculator – Compute AND OR XOR NOT Shifts
Perform bitwise operations on integers with our free online calculator. Supports AND, OR, XOR, NOT, left shift, and right shift with binary visualization for programming and computer science.
Bitwise Operations – Low-Level Binary Manipulation
Bitwise operations work directly on the binary representation of numbers. Instead of treating numbers as whole values, bitwise operators look at each individual bit (0 or 1) and apply logical rules. These operations are fundamental to programming – used in everything from graphics processing to encryption to data compression.
Programmers use bitwise operations for tasks like setting flags, checking permissions, manipulating colors, optimizing calculations, and working with hardware registers. While high-level code often hides these operations, understanding them gives you insight into how computers actually process data.
Bitwise Operators Explained
AND (&)
Returns 1 only when both bits are 1. Commonly used for masking – extracting specific bits from a number.
1100 & 1010 = 1000
OR (|)
Returns 1 if either bit is 1. Used for setting bits – turning specific flags on without affecting others.
1100 | 1010 = 1110
XOR (^)
Returns 1 when bits differ. Used for toggling bits, simple encryption, and detecting differences.
1100 ^ 1010 = 0110
NOT (~)
Inverts every bit. In two's complement, ~x = -(x+1). Used for creating masks and inverting flags.
(in 8-bit: ~00101010 = 11010101)
Left Shift (<<)
Moves all bits left by specified positions. Equivalent to multiplying by 2^n. Used for fast multiplication.
000101 << 2 = 010100 (5 × 4 = 20)
Right Shift (>>)
Moves all bits right by specified positions. Equivalent to dividing by 2^n (floor division). Used for fast division.
010100 >> 2 = 000101 (20 ÷ 4 = 5)
Common Use Cases
Bitmasking with AND
Extract specific bits using a mask. To get the last 4 bits of any number, AND it with 15 (binary 1111).
Setting Flags with OR
Turn on specific bits without affecting others. Common in permission systems and configuration flags.
Toggling with XOR
Flip specific bits. XOR with 1 toggles, XOR twice returns to original. Used in simple encryption and graphics.
Fast Multiplication/Division
Left shift multiplies by powers of 2. Right shift divides by powers of 2. Faster than multiplication on many processors.
Worked Examples
Example 1: AND Operation
Calculate 12 AND 10
Example 2: OR Operation
Calculate 12 OR 10
Example 3: XOR Operation
Calculate 12 XOR 10
Example 4: Left Shift
Calculate 5 left-shifted by 2
Example 5: Right Shift
Calculate 20 right-shifted by 2
Example 6: NOT Operation
Calculate NOT 42
Quick Fact
The XOR operation has a unique property: it's its own inverse. If A XOR B = C, then C XOR B = A. This makes XOR the basis of simple symmetric encryption and is why XOR appears in RAID data recovery, error correction codes, and cryptographic algorithms.
Frequently Asked Questions
What's the difference between bitwise and logical operators?
Bitwise operators (&, |, ~, ^) work on each bit of a number. Logical operators (&&, ||, !) work on boolean true/false values. For example, 5 & 3 = 1 (bitwise), but 5 && 3 = true (logical). Use bitwise for number manipulation, logical for conditions.
Why does NOT 42 equal -43?
Computers use two's complement for negative numbers. Inverting all bits of a positive number and adding 1 gives its negative. So ~x = -(x+1). For 42: flip all bits of 00101010 to get 11010101, which represents -43 in two's complement.
When should I use XOR?
XOR is great for toggling (flip bits where mask has 1s), comparing (result is 0 if values are equal), and simple encryption. It's also used in graphics for XOR cursors that invert whatever they're drawn over, and in checksums for error detection.
What happens with negative numbers?
Negative numbers use two's complement representation. Bitwise operations work the same way on the bit patterns. Right shift (>>) preserves the sign bit (arithmetic shift), so negative numbers stay negative. Left shift can overflow and change the sign.
How do I check if a specific bit is set?
Use AND with a mask that has 1 only at the bit position you care about. To check bit 3 (value 8 or 1000 in binary): if (number & 8) !== 0, then bit 3 is set. For bit n, use (number & (1 << n)) !== 0.
What's the difference between >> and >>>?
>> is arithmetic right shift – it preserves the sign bit, so negative numbers stay negative. >>> is logical right shift – it always fills with 0, treating the number as unsigned. In JavaScript, >>> converts to unsigned 32-bit.
Are bitwise operations faster than regular math?
On most modern processors, bitwise operations are extremely fast – often a single CPU cycle. Shifts can be faster than multiplication/division. However, modern compilers often optimize multiplication by powers of 2 into shifts automatically, so manual optimization is rarely necessary in high-level code.
Other Free Tools
Binary Addition & Subtraction Calculator – Compute in Base 2
Add and subtract binary numbers step by step with our free online binary calculator. See each bit-by-bit operation clearly – perfect for computer science and digital electronics.
Two's Complement Calculator – Convert to Twos Complement
Convert any integer to its two's complement binary form or decode two's complement back to decimal with our free online calculator. Supports various bit widths.
Number Base Converter – Binary, Octal, Decimal, Hex Converter
Convert numbers between binary, octal, decimal, and hexadecimal bases instantly with our free online number base converter. Perfect for computer science students and programmers.
Truth Table Generator – Create Logic Truth Tables Online
Generate truth tables for any logical expression with our free online truth table generator. Supports AND, OR, NOT, XOR, NAND, NOR, and implication operators for any number of variables.
Boolean Expression Evaluator – Evaluate Logic Expressions Online
Evaluate any Boolean expression for given variable values with our free online Boolean expression evaluator. Supports all logical operators including AND, OR, NOT, XOR, and more.
Logic Gate Simulator – Simulate AND OR NOT Gates Online
Simulate any combination of digital logic gates with our free online logic gate simulator. Set input values and see real-time output for AND, OR, NOT, NAND, NOR, and XOR gates.
Data Storage Converter – Convert KB, MB, GB, TB Online
Convert between any digital storage unit with our free online data storage converter. Supports bytes, kilobytes, megabytes, gigabytes, terabytes, and petabytes instantly.
BODMAS / PEMDAS Calculator – Order of Operations Solver
Solve any math expression using the correct order of operations with our free BODMAS/PEMDAS calculator. Get step-by-step breakdowns to understand exactly how each expression is evaluated.