Bitwise Calculator
Perform bitwise operations (AND, OR, XOR, NOT, shifts) on binary numbers instantly. Perfect for programmers and computer science students.
Bitwise Calculator - Perform Bitwise Operations on Binary Numbers
Our free online bitwise calculator allows you to perform bitwise operations (AND, OR, XOR, NOT, and shifts) on binary, decimal, or hexadecimal numbers. Bitwise operations are fundamental in computer programming, digital electronics, and low-level system design. This tool provides instant calculations with step-by-step explanations to help you understand how bitwise operations work.
What are Bitwise Operations?
Bitwise operations are operations that work directly on the binary representation of numbers, manipulating individual bits. Unlike arithmetic operations that work on the whole number, bitwise operations examine and modify each bit position independently. These operations are extremely fast and are used extensively in programming for flags, masks, and low-level optimizations.
How to Use the Bitwise Calculator
- Select Input Type: Choose binary, decimal, or hexadecimal as your input format
- Select Operation: Choose from AND (&), OR (|), XOR (^), NOT (~), Left Shift (<<), Right Shift (>>), or Zero-fill Right Shift (>>>)
- Enter First Number: Input your first number in the selected format
- Enter Second Number: For binary operations (except NOT), enter the second number
- Get Results: The calculator automatically computes the result in binary, decimal, and hexadecimal formats with detailed steps
Bitwise Operations Explained
AND Operation (&)
The AND operation returns 1 only when both bits are 1:
- 0 & 0 = 0
- 0 & 1 = 0
- 1 & 0 = 0
- 1 & 1 = 1
Example: 1010 & 1100 = 1000
Use Cases: Masking bits, checking if specific bits are set, clearing bits
OR Operation (|)
The OR operation returns 1 when at least one bit is 1:
- 0 | 0 = 0
- 0 | 1 = 1
- 1 | 0 = 1
- 1 | 1 = 1
Example: 1010 | 1100 = 1110
Use Cases: Setting bits, combining flags, merging bit patterns
XOR Operation (^)
The XOR (exclusive OR) operation returns 1 when bits are different:
- 0 ^ 0 = 0
- 0 ^ 1 = 1
- 1 ^ 0 = 1
- 1 ^ 1 = 0
Example: 1010 ^ 1100 = 0110
Use Cases: Toggling bits, encryption, finding unique elements, swapping values without temporary variables
NOT Operation (~)
The NOT operation inverts all bits (unary operation):
- ~0 = 1
- ~1 = 0
Example: ~1010 = 0101 (in 4-bit representation)
Note: In 32-bit systems, NOT performs two's complement, so ~n = -(n+1)
Use Cases: Inverting bit patterns, creating masks, complement operations
Left Shift (<<)
Left shift moves all bits to the left, filling with zeros on the right:
- Effectively multiplies the number by 2n where n is the shift amount
- Example: 1010 << 2 = 101000 (shifts left by 2 positions)
Example: 5 << 2 = 20 (5 × 2² = 20)
Use Cases: Fast multiplication by powers of 2, bit manipulation, creating bit masks
Right Shift (>>)
Right shift moves all bits to the right, preserving the sign bit (arithmetic shift):
- Effectively divides the number by 2n where n is the shift amount (for positive numbers)
- Preserves the sign bit for signed integers
- Example: 1010 >> 2 = 0010 (shifts right by 2 positions)
Example: 20 >> 2 = 5 (20 ÷ 2² = 5)
Use Cases: Fast division by powers of 2, extracting bit fields, arithmetic operations
Zero-fill Right Shift (>>>)
Zero-fill right shift moves all bits to the right, filling with zeros on the left (logical shift):
- Always fills with zeros, regardless of sign
- Treats the number as unsigned
- Example: -1 >>> 1 = 2147483647 (in 32-bit)
Use Cases: Treating numbers as unsigned, extracting bit fields without sign extension
Practical Applications
- Computer Programming: Bitwise operations are essential for flags, permissions, and low-level optimizations
- Digital Electronics: Used in logic gates, circuit design, and hardware control
- Cryptography: Many encryption algorithms use bitwise operations for mixing and permuting bits
- Graphics Programming: Manipulating pixel data, color channels, and image processing
- Network Protocols: Error detection, checksums, and protocol headers use bitwise operations
- Data Compression: Bit manipulation is fundamental in compression algorithms
- Embedded Systems: Direct hardware control and register manipulation
Common Bitwise Patterns
| Pattern | Binary | Decimal | Use Case |
|---|---|---|---|
| Set bit | x | (1 << n) | - | Set nth bit to 1 |
| Clear bit | x & ~(1 << n) | - | Set nth bit to 0 |
| Toggle bit | x ^ (1 << n) | - | Flip nth bit |
| Check bit | (x >> n) & 1 | - | Check if nth bit is set |
| Power of 2 | x & (x-1) == 0 | - | Check if number is power of 2 |
Bitwise Operation Truth Tables
| A | B | AND (&) | OR (|) | XOR (^) |
|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 |
| 0 | 1 | 0 | 1 | 1 |
| 1 | 0 | 0 | 1 | 1 |
| 1 | 1 | 1 | 1 | 0 |
Frequently Asked Questions
What are bitwise operations and why are they important?
Bitwise operations work directly on the binary representation of numbers, manipulating individual bits. They are fundamental in computer programming for flags, permissions, low-level optimizations, cryptography, graphics programming, and embedded systems. Bitwise operations are extremely fast and are used extensively in system-level programming.
What's the difference between AND, OR, and XOR operations?
AND (&) returns 1 only when both bits are 1. OR (|) returns 1 when at least one bit is 1. XOR (^) returns 1 when bits are different. AND is used for masking, OR for setting bits, and XOR for toggling bits or finding differences.
What's the difference between right shift (>>) and zero-fill right shift (>>>)?
Right shift (>>) is an arithmetic shift that preserves the sign bit, filling with the sign bit for signed integers. Zero-fill right shift (>>>) is a logical shift that always fills with zeros, treating the number as unsigned. Use >> for signed numbers and >>> when you want unsigned behavior.
Can I use decimal or hexadecimal numbers with the bitwise calculator?
Yes! Our calculator supports binary, decimal, and hexadecimal input formats. Simply select your preferred input type, and the calculator will automatically convert to the appropriate format for bitwise operations and display results in all three formats (binary, decimal, and hexadecimal).
How does the NOT operation work?
The NOT operation (~) is a unary operation that inverts all bits. In 32-bit systems, it performs two's complement, so ~n = -(n+1). For example, ~5 = -6. The calculator shows the result in binary, decimal, and hexadecimal formats with detailed step-by-step explanations.
What are common use cases for bitwise operations in programming?
Common use cases include: setting/clearing/toggling individual bits, checking if specific bits are set, creating bit masks, fast multiplication/division by powers of 2, flags and permissions systems, cryptography, graphics programming (pixel manipulation), network protocols (checksums, headers), and embedded systems (hardware control).
Does the calculator show step-by-step calculations?
Yes! The calculator displays detailed calculation steps including: conversion of input numbers to decimal, binary representation (32-bit), the bitwise operation performed, and results in binary, decimal, and hexadecimal formats. This helps you understand exactly how each operation works.
How do I check if a number is a power of 2 using bitwise operations?
You can check if a number is a power of 2 using: (n & (n-1)) == 0. This works because powers of 2 have only one bit set. For example, 8 (1000) & 7 (0111) = 0, confirming 8 is a power of 2. Our calculator can help you verify such patterns.
Related tools
Your recent visits