Free Online Engineering Tools
Home / Bitwise Calculator

Bitwise Calculator

Perform AND (&), OR (|), XOR (^), NOT (~), left shift (<<), right shift (>>) on any integer — live binary visualization, truth table, decimal/hex/octal output, bit-masking guide, and code examples for C, Python, Java & JavaScript.

🖩 Bitwise Calculator

Perform bitwise operations on integers and visualize the binary representation.

Input
Binary:
Binary:
Number Format
Bitwise Operations Results
OperationSymbolBinary Result (32-bit)Decimal ResultDescription
About Bitwise Operations

Bitwise operations work on individual bits of binary representations. They are used in low-level programming, cryptography, optimization, and hardware manipulation.

Fast
⌨️
Memory Efficient
🔌
Hardware Control
🛡️
Security
🚩
Flags & Permissions
Binary Visualization
A (5)
31282420161284
B (3)
31282420161284
Bitwise Table (A & B)
Common Examples
Check if a number is odd:5 & 1 = 1 (True)
Check if a number is even:4 & 1 = 0 (True)
Clear rightmost set bit:5 & (5−1) = 4
Toggle a bit (flip):5 ^ 1 = 4
Set a bit:5 | (1<<2) = 7
Clear a bit:7 & ~(1<<2) = 3
Quick Tips
x << 1 Multiply by 2
x >> 1 Divide by 2
x & 1 Check if odd
x ^ x Always 0
x & (x-1) Clear rightmost bit

Bitwise Calculator — Complete Guide to AND, OR, XOR, NOT & Bit Shifts

This bitwise calculator performs all six fundamental bitwise operations — AND (&), OR (|), XOR (^), NOT (~), left shift (<<), and right shift (>>) — on any integer with live binary visualization, decimal, hexadecimal, and octal output, a bit-by-bit truth table, and ready-to-use code examples for C, Python, Java, and JavaScript. Whether you are learning binary arithmetic, masking hardware registers, implementing permission flags, or debugging low-level code, this tool shows exactly which bits are set and what each operation produces at every bit position — instantly.

What Are Bitwise Operations?

Bitwise operations manipulate the individual binary digits (bits) of integers directly — treating each bit position independently rather than the number as a whole. They are among the fastest single-instruction operations any CPU can execute, and they form the backbone of low-level programming, embedded systems, network protocols, graphics, cryptography, and compression. Unlike arithmetic operations that carry values between bit positions, bitwise operations are purely positional and require no carry propagation.

Quick Reference: Complete Bitwise Truth Table

Bit ABit BA AND B (&)A OR B (|)A XOR B (^)NOT A (~A)NOT B (~B)
0000011
0101110
1001101
1111000

All Six Bitwise Operators — Symbol, Rule & Primary Use

OperatorSymbolRulePrimary Use CaseTwo-Operand?
AND&1 only if BOTH bits = 1Test / read / mask specific bitsYes
OR|1 if EITHER bit = 1Set specific bits to 1Yes
XOR^1 only if bits DIFFERToggle bits, parity, swap, cipherYes
NOT~Flip every bit (1→0, 0→1)Invert; ~x = -(x+1) in two's complementNo (unary)
Left Shift<<Shift bits left, fill right with 0Multiply by 2^n; build masksYes
Right Shift>>Shift bits right, discard low bitsInteger divide by 2^n; extract fieldsYes

Worked Examples

💻 Example 1 — AND, OR, XOR, NOT: A = 5, B = 3
GivenA = 5 = 0101  |  B = 3 = 0011   (4-bit representation)
AND0101 & 0011 = 0001 = 1   (bit 0 is 1 in both A and B only)
OR0101 | 0011 = 0111 = 7   (every bit that is 1 in A or B)
XOR0101 ^ 0011 = 0110 = 6   (bits 1 and 2 differ between A and B)
NOT A~0101 = 11111010 = -6 signed  /  250 unsigned 8-bit   (~x = -(x+1))
Summary5 & 3 = 1  |  5 | 3 = 7  |  5 ^ 3 = 6  |  ~5 = -6
🔧 Example 2 — Bit Masking: Read, Set, Clear & Toggle a Specific Bit
Givenvalue = 0b10110101 (181)  |  Want to operate on bit 3 (4th from right, value 8)
Readvalue & (1 << 3) = 10110101 & 00001000 = 00001000 → bit 3 is SET (non-zero)
Clearvalue & ~(1 << 3) = 10110101 & 11110111 = 10110101 → 10110001 = 177 → bit 3 cleared to 0
Set177 | (1 << 3) = 10110001 | 00001000 = 10111001 = 185 → bit 3 set back to 1
Togglevalue ^ (1 << 3) = 10110101 ^ 00001000 = 10111101 = 189 → bit 3 flipped
PatternRead: & mask  |  Clear: & ~mask  |  Set: | mask  |  Toggle: ^ mask
🚩 Example 3 — Permission Flags with Bitwise Operations
GivenREAD = 1 (001), WRITE = 2 (010), EXEC = 4 (100)  |  User has READ + EXEC = 5 (101)
Testperms & WRITE = 101 & 010 = 000 = 0 → user does NOT have write permission
Grantperms | WRITE = 101 | 010 = 111 = 7 → user now has all three permissions
Revokeperms & ~READ = 111 & 110 = 110 = 6 → WRITE + EXEC only, READ removed
ResultPack N booleans into 1 integer  |  Test with AND  |  Grant with OR  |  Revoke with AND NOT

Two's Complement and Signed Integers Explained

Most modern computers represent negative integers using two's complement encoding. To negate any integer: flip all bits (bitwise NOT), then add 1. The key rule for bitwise operations is ~x = -(x+1) for any signed integer. This means ~0 = -1, ~5 = -6, ~-1 = 0. Understanding this is essential when performing NOT operations or right-shifting signed negative numbers.

Decimal8-bit Unsigned Binary8-bit Signed (Two's Complement)NOT Result (~)
0000000000-1 (11111111)
1000000011-2 (11111110)
5000001015-6 (11111010)
12701111111127 (max signed)-128 (10000000)
12810000000-128127 (01111111)
25511111111-10 (00000000)

Common Bitwise Tricks & Idioms

TaskCode (C/Java/JS)How It Works
Test if evenx & 1 == 0Bit 0 is 0 for all even numbers
Test if power of 2x > 0 && (x & (x-1)) == 0Powers of 2 have exactly one set bit
Multiply by 2^nx << nLeft shift = fast multiply by power of 2
Divide by 2^nx >> nRight shift = floor division by power of 2
Swap two varsa^=b; b^=a; a^=bXOR is its own inverse: a^b^b = a
Set bit nx | (1 << n)OR with single-bit mask sets that bit
Clear bit nx & ~(1 << n)AND with inverted mask clears that bit
Toggle bit nx ^ (1 << n)XOR flips the target bit each time
Get lowest set bitx & (-x)-x = ~x+1; AND isolates lowest 1 bit
Clear lowest set bitx & (x-1)Subtracting 1 flips bits up to lowest 1
Count set bits (popcount)Integer.bitCount(x) / bin(x).count('1')Brian Kernighan: loop x &= x-1
Round up to power of 21 << (int)(Math.log(x)/Math.log(2)+1)Find next power of 2 via shift

Bitwise Operations in Programming Languages

LanguageANDORXORNOTLeft ShiftRight ShiftUnsigned Right
C / C++&|^~<<>> (impl-def signed)cast to unsigned
Java&|^~<<>> (arithmetic)>>>
JavaScript&|^~<<>> (arithmetic)>>>
Python&|^~<<>> (arithmetic)N/A (arbitrary prec.)
C#&|^~<<>> (arithmetic)>>> (C# 11+)
Rust&|^!<<>> (type-dependent)u-type >>

Practical Applications of Bitwise Operations

Permission & Access Control Systems

Unix file permissions (rwxrwxrwx = 9 bits), Linux capability flags, and database ACLs all store multiple boolean permissions in a single integer. Each flag occupies one bit position, allowing fast OR to grant, AND NOT to revoke, and AND to test permissions in one CPU instruction.

Hardware Register Programming

Microcontroller datasheets describe peripheral control registers as collections of named bits. Drivers read with AND, set with OR, and clear with AND NOT to configure timers, UART baud rates, GPIO directions, and interrupt enables without side-effects on neighbouring bits.

Network Protocols & Subnet Masking

IP subnet masking extracts the network address with AND: host_ip & subnet_mask. IPv4 header parsing uses shifts and masks to extract version (bits 28-31), IHL (bits 24-27), and DSCP (bits 18-23) fields from packed 32-bit words.

Colour Channel Manipulation

A packed ARGB colour is a 32-bit integer. Channels are extracted with right shifts and AND masks: alpha = (color >> 24) & 0xFF; red = (color >> 16) & 0xFF; green = (color >> 8) & 0xFF; blue = color & 0xFF. Reassembly uses left shifts and OR.

Cryptography & Hashing

XOR is the workhorse of stream ciphers — plaintext XOR keystream = ciphertext; ciphertext XOR keystream = plaintext. SHA-256, MD5, and most block ciphers (AES, DES) use AND, OR, XOR, and bit rotations as their primary non-linear mixing operations.

Frequently Asked Questions

What is the result of 5 AND 3?

5 & 3 = 1. In binary: 0101 & 0011 = 0001. Only bit position 0 is set in both numbers, so the result is 1.

Why does ~5 equal -6 and not -5?

Bitwise NOT flips every bit. In two's complement the rule is ~x = -(x+1). So ~5 = -(5+1) = -6. This is because flipping all bits then adding 1 gives the negative, so flipping all bits gives the negative minus 1.

What is the difference between & (bitwise AND) and && (logical AND)?

Bitwise AND (&) operates on every bit pair and returns an integer. Logical AND (&&) treats each operand as true/false, short-circuits if the first is false, and returns a boolean. Use & for bit manipulation and && for conditional logic.

What is XOR used for?

XOR is self-inverse (x^y^y = x), making it ideal for: toggling specific bits, swapping two variables without a temporary variable, simple stream ciphers, parity bits in error detection, and hash mixing in MD5/SHA/MurmurHash.

How does bit masking work?

A mask is an integer where specific bits are 1 and the rest are 0 (or vice versa). AND with a mask reads those bits. OR with a mask sets those bits. XOR with a mask toggles those bits. AND with the inverted mask clears those bits.

Related Calculators