NUMBER THEORYBinaryMathematics Calculator
01

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.

Concept Fundamentals
0, 1
Digits
8
Bits/Byte
256
8-bit Values
32/64
CPU Widths
Start CalculatingArithmetic and logic: add, subtract, multiply, divide, AND, OR, XOR, NOT, shifts

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.
01

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

Please provide the first number

โš ๏ธ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?

๐Ÿ”ขEvery modern CPU performs billions of binary operations per second โ€” addition is the most fundamentalSource: Computer Architecture
๐Ÿ”XOR is the basis of many encryption algorithms โ€” XORing with a key encrypts, XORing again decryptsSource: Cryptography
โšกLeft shift by 1 multiplies by 2; right shift by 1 divides by 2 โ€” faster than multiplication/divisionSource: Compiler Optimization
๐ŸŽฏAND with a mask extracts specific bits โ€” e.g., AND 0x0F gets the lower 4 bits (nibble)Source: Bit Manipulation
๐Ÿ“ŠBinary division in hardware uses repeated subtraction โ€” similar to long division you learned in schoolSource: Digital Logic
๐ŸŒIPv4 addresses are 32-bit binary numbers โ€” subnet masks use AND to determine network vs hostSource: Networking
๐Ÿ’พFile permissions (rwx) use bitwise OR to combine and AND to check โ€” e.g., chmod 755Source: Unix/Linux

๐Ÿ“– 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

OperationSymbolInputsUse Case
Addition+2Sums, memory addressing
Subtraction-2Differences, pointer arithmetic
Multiplicationร—2Scaling, area calculation
Divisionรท2Ratios, resource allocation
ANDโˆง2Masking, extract bits
ORโˆจ2Set bits, combine flags
XORโŠ•2Toggle, encryption, parity
NOTยฌ1Invert, complement
Left Shift<<2Multiply by 2^n
Right Shift>>2Divide 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

2
Digits (0,1)
8
Bits = 1 Byte
256
Values in 8-bit
32/64
Common CPU widths

โš ๏ธ 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.

๐Ÿ‘ˆ START HERE
โฌ…๏ธJump in and explore the concept!
AI