Binary Math & Bitwise Logic
Welcome to the ultimate programmer's toolkit. Whether you are writing low-level C code, analyzing subnet masks, or studying computer architecture, understanding binary logic and floating-point memory structures is critical.
Introduction
Computers use binary (0s and 1s). Humans use decimal (0-9). Programmers use hexadecimal (0-9, A-F) as shorthand for binary. This calculator bridges all formats, handling signed two's complement representation, bitwise logic, and IEEE 754 floating-point numbers with support for adjustable bit lengths (8/16/32/64 bits).
Core Logic & Conversions
Binary to Decimal
Each bit represents a power of 2:
1011₂ = (1×2³) + (0×2²) + (1×2¹) + (1×2⁰)
= 8 + 0 + 2 + 1 = 11₁₀
Signed Integers (Two's Comp)
The leftmost bit indicates sign (0=positive, 1=negative). To find -5 in 8-bit:
1. Positive 5: 00000101
2. Flip bits: 11111010
3. Add 1: 11111011 = -5
How Bitwise Operations Work
- AND (&): Returns 1 only if BOTH bits are 1. Used for masking (e.g., checking flags).
- OR (|): Returns 1 if EITHER bit is 1. Used to set specific bits on.
- XOR (^): Returns 1 if the bits are DIFFERENT. Used in cryptography and toggling states.
- NOT (~): Flips all bits (0 becomes 1, 1 becomes 0).
- Shift (<< or >>): Moves all bits to the left or right, effectively multiplying or dividing by 2 instantly.
IEEE 754 Floating Point (32-bit)
Computers cannot natively store decimal points. To store a number like -118.625, they break the memory into three distinct parts:
- Sign Bit (1 bit): 0 = positive, 1 = negative.
- Exponent (8 bits): A biased integer (E = exponent + 127) that scales the number.
- Mantissa / Fraction (23 bits): Represents the precise decimal digits (significant digits).
Real-Life Examples
Hex: C0.A8.01.01
FF(16) = 255(10)
Tips & Best Practices
- For Programmers: Use hex for debugging memory addresses and bit flags. Use AND to test flags, OR to set them, and XOR to toggle.
- For Students: Memorize the powers of 2 (2⁰=1 up to 2¹⁰=1024). Remember that exactly 4 binary bits make up 1 hex digit.
Frequently Asked Questions
Q: What's the difference between signed and unsigned integers?
A: Unsigned numbers are only positive (0 to 2ⁿ-1). Signed numbers can be positive or negative (-2ⁿ⁻¹ to 2ⁿ⁻¹-1) using two's complement.
Q: Why does 0.1 + 0.2 not equal 0.3 in floating point?
A: Binary cannot represent fractions like 0.1 exactly (it creates a repeating binary decimal). The actual stored value is approximately 0.10000000000000000555.
Q: What is ASCII?
A: American Standard Code for Information Interchange. It maps 7-bit binary numbers to specific printable characters (0-127).