Bitwise Calculator

Bitwise Calculator

Perform bitwise operations on integers including AND, OR, XOR, NOT, and bit shifts. Essential for low-level programming and digital logic.

2026-03-28T00:00:00Z

Result will appear here...

What are Bitwise Operations?

Bitwise operations are operations that directly manipulate bits, which are the smallest units of data in computing. These operations work at the binary level, treating numbers as sequences of bits (0s and 1s) rather than decimal values.

Bitwise operations are fundamental in computer science and are used extensively in low-level programming, embedded systems, cryptography, graphics programming, and performance optimization. They allow programmers to perform operations much faster than arithmetic operations and can reduce memory usage.

Common bitwise operations include AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>). Each operation has specific uses, from setting and clearing flags to multiplying or dividing by powers of two.

Input Validation: This calculator requires valid integers for all inputs. Decimal values are truncated to integers (the decimal part is discarded). Empty or non-numeric inputs are rejected with an error message.

Bitwise Operations Explained

The Six Basic Operations

AND (&)

Returns 1 only if both bits are 1. Used for masking and checking if specific bits are set.

12 & 5 = 4 (1100 & 0101 = 0100)
OR (|)

Returns 1 if either bit is 1. Used for setting bits and combining flags.

12 | 5 = 13 (1100 | 0101 = 1101)
XOR (^)

Returns 1 if bits are different. Used for toggling bits and simple encryption.

12 ^ 5 = 9 (1100 ^ 0101 = 1001)
NOT (~)

Inverts all bits (0 becomes 1, 1 becomes 0). Creates the bitwise complement. Note: NOT requires only one operand (Number 1); Number 2 is ignored.

~12 = -13 (inverts all 32/64 bits)
LEFT SHIFT (<<)

Shifts bits left N positions (where N = Number 2), filling with zeros. Equivalent to multiplying by 2^N.

12 << 2 = 48 (1100 << 2 = 110000)
RIGHT SHIFT (>>)

Shifts bits right N positions (where N = Number 2), discarding rightmost bits. Equivalent to dividing by 2^N and rounding down.

12 >> 2 = 3 (1100 >> 2 = 0011)

Example: AND Operation

Let's perform 12 & 5:

Given:
Number 1: 12
Number 2: 5
Step 1:
Convert to binary:
12 = 0000 1100
5 = 0000 0101
Step 2:
Apply AND operation (bit by bit):
0000 1100
& 0000 0101
───────────
0000 0100
Step 3:
Convert back to decimal:
0000 0100 = 4
Final Result:
4
Binary: 0100 | Hex: 0x4

Frequently Asked Questions

What is the difference between & and &&?

& is a bitwise AND that operates on individual bits, while && is a logical AND that operates on boolean values. The single & always evaluates both operands, whereas && short-circuits if the first operand is false.

Why does ~12 equal -13?

The NOT operator inverts all bits. In two's complement representation (used for signed integers), inverting all bits of a positive number n gives -(n+1). So ~12 = -(12+1) = -13.

How do shifts relate to multiplication?

Left shift by n positions multiplies by 2^n. For example, 5 << 2 = 5 × 4 = 20. Right shift by n divides by 2^n and rounds down. This makes shifts faster than multiplication/division operations.

What is bit masking?

Bit masking uses AND operations to extract specific bits from a number. For example, n & 0xFF extracts the lowest 8 bits. It's commonly used to isolate specific data within a packed integer.

When should I use XOR?

XOR is useful for toggling bits (x ^ 1 toggles the least significant bit), swapping values without a temporary variable (a ^= b; b ^= a; a ^= b), and simple encryption/checksums.

Are bitwise operations faster?

Yes! Bitwise operations are among the fastest CPU operations. They're often used in performance-critical code, embedded systems, and low-level programming where every cycle counts.

What are common use cases?

Bitwise operations are used for: setting/clearing flags, permissions systems, network protocols, graphics (color manipulation), compression algorithms, hash functions, and optimizing mathematical operations.

How do I set or clear specific bits?

To set bit n: use x | (1 << n). To clear bit n: use x & ~(1 << n). To toggle bit n: use x ^ (1 << n). To check if bit n is set: use (x & (1 << n)) != 0.

Related Tools