Binary Operations Toolkit
Every modern CPU performs billions of binary operations per second. Addition, subtraction, multiplication, division, AND, OR, XOR, NOT, and shifts โ all in one place.
Why This Mathematical Concept Matters
Why: Binary operations form the foundation of all digital computing โ from simple addition to cryptography.
How: Arithmetic: column-by-column with carries/borrows. Logic: bit-by-bit AND, OR, XOR, NOT. Shifts: multiply/divide by 2โฟ.
- โXOR is reversible: (A โ B) โ B = A โ basis of stream ciphers.
- โx % 8 = x & 7. AND with (2โฟ-1) extracts lower n bits.
- โLeft shift ร1 = multiply by 2; right shift ร1 = divide by 2.
Binary Operations โ Complete Arithmetic and Logic Toolkit
Addition, subtraction, multiplication, division, AND, OR, XOR, NOT, and bit shifts. All in one place.
๐ Examples โ Click to Load
Operation & Format
Operands
โ ๏ธFor educational and informational purposes only. Verify with a qualified professional.
๐งฎ Fascinating Math Facts
Every modern CPU performs billions of binary operations per second.
โ Computer Architecture
XOR is the basis of many encryption algorithms.
โ Cryptography
๐ Key Takeaways
- โข Addition: 0+0=0, 0+1=1, 1+0=1, 1+1=10 (carry 1)
- โข Subtraction: Uses borrow; negative results use two's complement
- โข Multiplication: Simpler than decimal โ 0รanything=0, 1ร1=1
- โข Division: Long division in binary; produces quotient and remainder
- โข AND: 1 only when both bits are 1 โ used for masking
- โข OR: 1 when either bit is 1 โ used for setting bits
- โข XOR: 1 when bits differ โ used for toggling and encryption
- โข NOT: Flips all bits โ one's complement
- โข Shifts: Left = multiply by 2^n, Right = divide by 2^n
๐ก Did You Know?
๐ How Binary Operations Work
Binary operations form the foundation of all digital computing. Computers represent data as sequences of 0s and 1s (bits), and every calculation โ from simple addition to complex cryptography โ is built from these operations.
Arithmetic Operations
Addition, subtraction, multiplication, and division follow the same logical rules as decimal arithmetic but with only two digits. Carries and borrows propagate from right to left. Multiplication is implemented as repeated addition with shifts; division as repeated subtraction.
Logical (Bitwise) Operations
AND, OR, XOR, and NOT operate on each bit independently. They are used for masking (AND), setting flags (OR), toggling (XOR), and inversion (NOT). These operations are extremely fast in hardware โ a single CPU cycle can process 64 or 128 bits at once.
Shift Operations
Left shift moves bits left, effectively multiplying by 2 for each position. Right shift moves bits right, dividing by 2. These are among the fastest operations a CPU can perform and are used for efficient multiplication/division by powers of 2.
๐ฏ Expert Tips
๐ก Use AND for Modulo by Powers of 2
x % 8 is equivalent to x & 7. AND with (2^n - 1) extracts the lower n bits, which equals modulo 2^n.
๐ก XOR Swaps Without Temp Variable
a ^= b; b ^= a; a ^= b; swaps two integers without a temporary variable. Useful in low-level code.
๐ก Check Even/Odd with AND 1
(x & 1) === 0 means even, (x & 1) === 1 means odd. Faster than x % 2.
๐ก Set/Clear/Toggle Bits
Set: x | (1 << n). Clear: x & ~(1 << n). Toggle: x ^ (1 << n). Essential for embedded systems.
โ๏ธ Binary Operations Comparison
| Operation | Symbol | Inputs | Use Case |
|---|---|---|---|
| Addition | + | 2 | Sums, memory addressing |
| Subtraction | - | 2 | Differences, pointer arithmetic |
| Multiplication | ร | 2 | Scaling, area calculation |
| Division | รท | 2 | Ratios, resource allocation |
| AND | โง | 2 | Masking, extract bits |
| OR | โจ | 2 | Set bits, combine flags |
| XOR | โ | 2 | Toggle, encryption, parity |
| NOT | ยฌ | 1 | Invert, complement |
| Left Shift | << | 2 | Multiply by 2^n |
| Right Shift | >> | 2 | Divide by 2^n |
โ Frequently Asked Questions
What is binary addition and how does carry work?
Binary addition works like decimal: add digits from right to left. 0+0=0, 0+1=1, 1+0=1, 1+1=10 (write 0, carry 1). The carry propagates to the next column. Overflow occurs when the result exceeds the bit width.
How does binary subtraction handle borrowing?
When subtracting 1 from 0, you borrow from the next left column (borrow 2 in binary). Negative results are typically represented in two's complement form, where the leftmost bit indicates sign.
Why is XOR used in encryption?
XOR has a unique property: (A โ B) โ B = A. Encrypt by XORing data with a key; decrypt by XORing again with the same key. This is the basis of stream ciphers and one-time pads.
What is the difference between logical and arithmetic right shift?
Arithmetic right shift preserves the sign bit (for signed numbers); logical right shift fills with zeros. In JavaScript, >>> is logical; >> is arithmetic for negative numbers.
How do I extract specific bits with AND?
Create a mask with 1s in the positions you want: to get lower 4 bits, use AND 0x0F (binary 00001111). To get bit n, use AND (1 << n).
What is two's complement?
Two's complement is how computers represent negative integers. To get -x: invert all bits (NOT) and add 1. The leftmost bit is the sign bit: 0 = positive, 1 = negative.
Why are bitwise operations faster than arithmetic?
Bitwise operations map directly to single CPU instructions. No carry propagation for AND/OR/XOR. Shifts are especially fast โ often one cycle. Multiplication and division take multiple cycles.
How does binary multiplication work?
Same as decimal long multiplication but simpler: 0รanything=0, 1ร1=1. For each 1 in the multiplier, add a shifted copy of the multiplicand. Sum all partial products.
What is the maximum value for n bits?
Unsigned: 2^n - 1 (e.g., 8 bits = 255). Signed two's complement: -2^(n-1) to 2^(n-1)-1 (e.g., 8 bits = -128 to 127).
๐ Binary by the Numbers
๐ Official Data Sources
โ ๏ธ Disclaimer: This calculator is for educational purposes. Results assume unsigned or two's complement interpretation as appropriate. For signed operations, overflow/underflow behavior may vary by system. Always verify critical calculations. Not a substitute for professional software development practices.