Encoding

Bitwise Calculator Online — AND, OR, XOR, NOT with Examples

📅 June 8, 2026⏱ 7 min read 🛠️ Try the Bitwise Calculator →

Bitwise operations are fundamental in programming — used in permission systems, cryptography, flags, masks and low-level optimization. This guide explains every bitwise operator with clear examples in JavaScript, Python and C#.

What Are Bitwise Operations?

Bitwise operations work directly on the binary representation of numbers — operating on individual bits (0s and 1s) rather than the number as a whole. They are extremely fast because processors execute them in a single CPU cycle.

Common uses of bitwise operations:

👉 Don't want to calculate manually? Use our free Bitwise Calculator — performs AND, OR, XOR, NOT, left shift and right shift instantly in your browser.

Understanding Binary Numbers

Before bitwise operations make sense, you need to understand binary. Every number has a binary representation using only 0s and 1s:

DecimalBinary (8-bit)Hex
0000000000x00
1000000010x01
5000001010x05
10000010100x0A
15000011110x0F
255111111110xFF

Bitwise AND (&)

AND returns 1 only when both corresponding bits are 1. Otherwise returns 0.

  5 = 00000101
  3 = 00000011
AND = 00000001  →  1

Truth table:
0 AND 0 = 0
0 AND 1 = 0
1 AND 0 = 0
1 AND 1 = 1  ← only this returns 1

Real use case — check if a number is even or odd:

// JavaScript
console.log(5 & 1);  // 1 → odd
console.log(4 & 1);  // 0 → even

// Check permission bit
const READ    = 0b100;  // 4
const WRITE   = 0b010;  // 2
const EXECUTE = 0b001;  // 1

const userPermission = 0b110;  // READ + WRITE = 6
console.log(userPermission & READ);    // 4 → has read permission
console.log(userPermission & EXECUTE); // 0 → no execute permission

Bitwise OR (|)

OR returns 1 when at least one corresponding bit is 1.

  5 = 00000101
  3 = 00000011
 OR = 00000111  →  7

Truth table:
0 OR 0 = 0
0 OR 1 = 1
1 OR 0 = 1
1 OR 1 = 1

Real use case — combining permission flags:

// JavaScript
const READ    = 0b100;  // 4
const WRITE   = 0b010;  // 2
const EXECUTE = 0b001;  // 1

// Grant read and write permissions
const permissions = READ | WRITE;
console.log(permissions); // 6 (binary: 110)
console.log(permissions.toString(2)); // "110"

// Python
permissions = 0b100 | 0b010  # READ | WRITE
print(bin(permissions))  # 0b110

Bitwise XOR (^)

XOR (exclusive OR) returns 1 when bits are different. Returns 0 when bits are the same.

  5 = 00000101
  3 = 00000011
XOR = 00000110  →  6

Truth table:
0 XOR 0 = 0  ← same → 0
0 XOR 1 = 1  ← different → 1
1 XOR 0 = 1  ← different → 1
1 XOR 1 = 0  ← same → 0

Real use cases — XOR is incredibly powerful:

// 1. Swap two variables without a temp variable
let a = 5, b = 3;
a = a ^ b;  // a = 6
b = a ^ b;  // b = 5
a = a ^ b;  // a = 3
console.log(a, b); // 3, 5 ✓

// 2. Toggle a bit
let flags = 0b1010;
flags ^= 0b0010;  // toggle bit 1
console.log(flags.toString(2)); // "1000"

// 3. Simple encryption (XOR cipher)
const key = 42;
const message = 72;  // ASCII 'H'
const encrypted = message ^ key;  // 98
const decrypted = encrypted ^ key; // 72 → back to 'H'

// 4. Find the only non-duplicate in array
const nums = [1, 2, 3, 2, 1];
const unique = nums.reduce((acc, n) => acc ^ n, 0);
console.log(unique); // 3

Bitwise NOT (~)

NOT inverts every bit — 0 becomes 1, 1 becomes 0. In most languages this also depends on the integer size (two's complement).

  5 = 00000101
NOT = 11111010  →  -6 (in two's complement 32-bit)

// JavaScript
console.log(~5);   // -6
console.log(~0);   // -1
console.log(~-1);  // 0

// Formula: ~n = -(n + 1)
// ~5 = -(5 + 1) = -6

// Useful trick — fast floor for positive numbers
console.log(~~3.7);  // 3 (double NOT = floor)
console.log(~~-3.7); // -3

Left Shift (<<)

Shifts all bits to the left by N positions. Equivalent to multiplying by 2^N.

  5 = 00000101
5 << 1 = 00001010  →  10  (5 × 2)
5 << 2 = 00010100  →  20  (5 × 4)
5 << 3 = 00101000  →  40  (5 × 8)

// JavaScript
console.log(1 << 0);  // 1
console.log(1 << 1);  // 2
console.log(1 << 2);  // 4
console.log(1 << 3);  // 8
console.log(1 << 4);  // 16

// Fast multiplication by powers of 2
const x = 100;
console.log(x << 1);  // 200 (x * 2)
console.log(x << 3);  // 800 (x * 8)

Right Shift (>>)

Shifts all bits to the right by N positions. Equivalent to dividing by 2^N (integer division).

  20 = 00010100
20 >> 1 = 00001010  →  10  (20 / 2)
20 >> 2 = 00000101  →   5  (20 / 4)

// JavaScript
console.log(20 >> 1);  // 10
console.log(20 >> 2);  // 5
console.log(7  >> 1);  // 3 (integer division: 7/2 = 3.5 → 3)

// Extract RGB components from hex color
const color = 0xFF8833;
const red   = (color >> 16) & 0xFF;  // 255
const green = (color >> 8)  & 0xFF;  // 136
const blue  =  color        & 0xFF;  // 51

Bitwise Operations in JavaScript

// All bitwise operators in JavaScript
const a = 5;   // 0101
const b = 3;   // 0011

console.log(a & b);   // AND  → 1  (0001)
console.log(a | b);   // OR   → 7  (0111)
console.log(a ^ b);   // XOR  → 6  (0110)
console.log(~a);      // NOT  → -6
console.log(a << 1);  // LEFT SHIFT  → 10
console.log(a >> 1);  // RIGHT SHIFT → 2

// Convert to binary string
console.log((a & b).toString(2));  // "1"
console.log((a | b).toString(2));  // "111"
console.log((a ^ b).toString(2));  // "110"

// Parse binary string to number
const binary = parseInt('1010', 2);  // 10

Bitwise Operations in Python

a = 5   # 0b0101
b = 3   # 0b0011

print(a & b)    # AND  → 1
print(a | b)    # OR   → 7
print(a ^ b)    # XOR  → 6
print(~a)       # NOT  → -6
print(a << 1)   # LEFT SHIFT  → 10
print(a >> 1)   # RIGHT SHIFT → 2

# Display in binary
print(bin(a & b))  # 0b1
print(bin(a | b))  # 0b111
print(bin(a ^ b))  # 0b110

# Python supports arbitrarily large integers!
big = 1 << 100  # 2^100 — no overflow!

Bitwise Operations in C# / .NET

int a = 5;   // 0101
int b = 3;   // 0011

Console.WriteLine(a & b);    // AND  → 1
Console.WriteLine(a | b);    // OR   → 7
Console.WriteLine(a ^ b);    // XOR  → 6
Console.WriteLine(~a);       // NOT  → -6
Console.WriteLine(a << 1);   // LEFT SHIFT  → 10
Console.WriteLine(a >> 1);   // RIGHT SHIFT → 2

// Display as binary
Console.WriteLine(Convert.ToString(a & b, 2));  // "1"
Console.WriteLine(Convert.ToString(a | b, 2));  // "111"

// Flags enum — uses bitwise operations under the hood
[Flags]
public enum Permission
{
    None    = 0,      // 000
    Read    = 1 << 0, // 001
    Write   = 1 << 1, // 010
    Execute = 1 << 2  // 100
}

var perm = Permission.Read | Permission.Write;
Console.WriteLine(perm.HasFlag(Permission.Read));    // True
Console.WriteLine(perm.HasFlag(Permission.Execute)); // False

Real-World Use Cases

Use caseOperationExample
Check if even/oddANDn & 1 == 0 → even
Multiply by power of 2Left shiftn << 3 = n × 8
Divide by power of 2Right shiftn >> 2 = n ÷ 4
Toggle a bitXORflags ^= mask
Set a bitORflags |= mask
Clear a bitAND NOTflags &= ~mask
Check a bitAND(flags & mask) != 0
Swap without tempXORa^=b; b^=a; a^=b
Extract RGB from hexShift + AND(color >> 16) & 0xFF
IP subnet maskANDip & subnet

Frequently Asked Questions

What is the difference between & and &&?
& is the bitwise AND operator — operates on individual bits. && is the logical AND operator — operates on boolean values and short-circuits (stops evaluating if the first operand is false).

What does ~5 equal?
In most languages using 32-bit two's complement integers, ~5 equals -6. The formula is ~n = -(n + 1).

Why does left shift multiply by 2?
In binary, shifting all bits one position to the left doubles the value — the same way shifting digits in decimal (multiplying by 10) increases the value tenfold.

What is XOR used for in cryptography?
XOR is the basis of many encryption algorithms because it's perfectly reversible — XORing a value twice with the same key returns the original value. This property makes it useful for stream ciphers and one-time pads.

How do I calculate bitwise operations online?
Use our free Bitwise Calculator — enter any two numbers, choose your operation (AND, OR, XOR, NOT, shift) and see the result in binary, decimal and hex instantly. Runs 100% in your browser.

Try the free Bitwise Calculator

Perform AND, OR, XOR, NOT, left shift and right shift instantly. See results in binary, decimal and hex. Runs 100% in your browser.

Bitwise Calculator →