TFT

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.

Examples:

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 (&)

1 & 1 = 1, otherwise 0

Returns 1 only when both bits are 1. Commonly used for masking – extracting specific bits from a number.

Example: 12 & 10 = 8
1100 & 1010 = 1000

OR (|)

0 | 0 = 0, otherwise 1

Returns 1 if either bit is 1. Used for setting bits – turning specific flags on without affecting others.

Example: 12 | 10 = 14
1100 | 1010 = 1110

XOR (^)

Same bits = 0, different = 1

Returns 1 when bits differ. Used for toggling bits, simple encryption, and detecting differences.

Example: 12 ^ 10 = 6
1100 ^ 1010 = 0110

NOT (~)

Flips all bits: 0→1, 1→0

Inverts every bit. In two's complement, ~x = -(x+1). Used for creating masks and inverting flags.

Example: ~42 = -43
(in 8-bit: ~00101010 = 11010101)

Left Shift (<<)

Shifts bits left, fills with 0

Moves all bits left by specified positions. Equivalent to multiplying by 2^n. Used for fast multiplication.

Example: 5 << 2 = 20
000101 << 2 = 010100 (5 × 4 = 20)

Right Shift (>>)

Shifts bits right, preserves sign

Moves all bits right by specified positions. Equivalent to dividing by 2^n (floor division). Used for fast division.

Example: 20 >> 2 = 5
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).

255 & 15 = 15
11111111 & 00001111 = 00001111
Useful for extracting color channels, flags, or nibbles

Setting Flags with OR

Turn on specific bits without affecting others. Common in permission systems and configuration flags.

permissions = permissions | READ_FLAG
If READ_FLAG = 4 (binary 100)
5 | 4 = 5 | 100 = 101 | 100 = 101 = 5 (already set)
1 | 4 = 1 | 100 = 001 | 100 = 101 = 5 (now set)

Toggling with XOR

Flip specific bits. XOR with 1 toggles, XOR twice returns to original. Used in simple encryption and graphics.

value ^ mask toggles bits where mask has 1s
10 ^ 1 = 11, then 11 ^ 1 = 10 (back to original)
XOR encryption: encrypted = data ^ key
decrypted = encrypted ^ key = data

Fast Multiplication/Division

Left shift multiplies by powers of 2. Right shift divides by powers of 2. Faster than multiplication on many processors.

x << 1 = x × 2
x << 3 = x × 8
x >> 1 = x ÷ 2 (floor)
x >> 4 = x ÷ 16 (floor)

Worked Examples

Example 1: AND Operation

Calculate 12 AND 10

12 in binary: 1100
10 in binary: 1010
Bit-by-bit AND:
1 AND 1 = 1
1 AND 0 = 0
0 AND 1 = 0
0 AND 0 = 0
Result: 1000 = 8

Example 2: OR Operation

Calculate 12 OR 10

12 in binary: 1100
10 in binary: 1010
Bit-by-bit OR:
1 OR 1 = 1
1 OR 0 = 1
0 OR 1 = 1
0 OR 0 = 0
Result: 1110 = 14

Example 3: XOR Operation

Calculate 12 XOR 10

12 in binary: 1100
10 in binary: 1010
Bit-by-bit XOR:
1 XOR 1 = 0 (same)
1 XOR 0 = 1 (different)
0 XOR 1 = 1 (different)
0 XOR 0 = 0 (same)
Result: 0110 = 6

Example 4: Left Shift

Calculate 5 left-shifted by 2

5 in binary: 000101
Shift left by 2 positions:
000101 << 2 = 010100
010100 = 16 + 4 = 20
Verification: 5 × 2^2 = 5 × 4 = 20 ✓

Example 5: Right Shift

Calculate 20 right-shifted by 2

20 in binary: 010100
Shift right by 2 positions:
010100 >> 2 = 000101
000101 = 4 + 1 = 5
Verification: floor(20 / 2^2) = floor(20/4) = 5 ✓

Example 6: NOT Operation

Calculate NOT 42

42 in binary (8-bit): 00101010
Invert all bits: 11010101
In two's complement, this equals -43
Rule: ~x = -(x + 1)
~42 = -(42 + 1) = -43 ✓

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.