Number System Converter & Bitwise Calculator

Convert between binary, decimal, hexadecimal, octal. Perform bitwise operations: AND, OR, XOR, shifts.

Number Systems

Bitwise Operations

Detailed Bit-Level Calculations

What is Number System Converter?

A Number System Converter is a tool that converts numbers between different bases (radix) including binary (base 2), decimal (base 10), hexadecimal (base 16), and octal (base 8). This tool is essential for programmers, computer science students, and anyone working with different number representations. Understanding number systems is fundamental in computer science as computers internally use binary, while programmers often work with hexadecimal for memory addresses and octal for file permissions.

Why Number Systems Matter in Computing

Different number systems serve specific purposes in computing. Binary is the foundation of all digital electronics and computer operations. Hexadecimal provides a more compact representation of binary data and is commonly used in memory addresses, color codes, and debugging. Octal is used in Unix file permissions. Understanding these systems is crucial for low-level programming, debugging, network programming, and understanding how computers store and process data.

Number Systems Explained

Binary (Base 2): Uses only 0 and 1. Each digit represents a power of 2. Example: 1010₂ = 10₁₀

Decimal (Base 10): Standard number system using digits 0-9. Each position represents a power of 10.

Hexadecimal (Base 16): Uses 0-9 and A-F. Compact representation of binary. Example: FF₁₆ = 255₁₀

Octal (Base 8): Uses digits 0-7. Each digit represents 3 binary bits. Example: 377₈ = 255₁₀

Bitwise Operations

Bitwise operations manipulate individual bits of binary numbers. These operations are extremely fast and commonly used in low-level programming, cryptography, graphics, and optimization.

AND (&): Returns 1 if both bits are 1. Example: 1010 & 1100 = 1000. Used for masking bits.

OR (|): Returns 1 if at least one bit is 1. Example: 1010 | 1100 = 1110. Used for setting bits.

XOR (^): Returns 1 if bits are different. Example: 1010 ^ 1100 = 0110. Used for toggling and encryption.

NOT (~): Inverts all bits. Example: ~1010 = 0101. One's complement operation.

Left Shift (<<): Shifts bits left, fills with 0. Example: 1010 << 2 = 101000. Multiplies by 2ⁿ.

Right Shift (>>): Shifts bits right, preserves sign. Example: 1010 >> 2 = 0010. Divides by 2ⁿ.

Unsigned Right Shift (>>>): Shifts right, fills with 0. Ignores sign bit.

Number Representation in Computer

Computers represent numbers using different encoding schemes depending on whether the number is signed, unsigned, or floating-point. Understanding these representations is crucial for low-level programming and avoiding bugs.

Unsigned Integer: Simple binary representation. 8-bit range: 0 to 255. Example: 11111111₂ = 255

Sign-Magnitude: MSB for sign (0=positive, 1=negative). Example: 8-bit -5 = 10000101. Problem: Two zeros (+0, -0)

One's Complement: Negate by inverting bits. Example: -5 = ~00000101 = 11111010. Still has two zeros.

Two's Complement (Most Common): Negate by inverting bits + 1. Example: -5 = ~00000101 + 1 = 11111011. Range: -128 to 127 (8-bit). Only one zero. Addition/subtraction use same circuit.

Excess-K (Biased): Add bias (2^(n-1)) to value. Used in IEEE 754 exponent. Example: Excess-127 for float exponent.

IEEE 754 Floating-Point: Sign(1) + Exponent(8) + Mantissa(23) for 32-bit float. Example: -12.5 = 1 10000010 10010000...00

Two's Complement Calculation

Step 1: Write positive number in binary

Step 2: Invert all bits (0→1, 1→0)

Step 3: Add 1 to the result

Example: -12 in 8-bit
1. 12 = 00001100
2. Invert: 11110011
3. Add 1: 11110100 = -12

How CPU Performs Arithmetic

Modern CPUs use specialized hardware circuits for arithmetic. Understanding this helps optimize code and avoid pitfalls.

ALU

ALU (Arithmetic Logic Unit): Core component performing arithmetic and logic operations. Contains adders, subtractors, multipliers, shifters.

Full Adder

Full Adder Circuit: Basic building block for addition. Inputs: A, B, Carry-in. Outputs: Sum, Carry-out. Formula: Sum=A⊕B⊕Cin, Cout=(A∧B)∨(Cin∧(A⊕B))

Carry Look-ahead

Carry Look-ahead Adder: Speeds up addition by computing all carries in parallel instead of ripple. Reduces delay from O(n) to O(log n).

Multiplication

Booth's Algorithm: Efficient multiplication. Reduces partial products by encoding multiplier bits. Example: 0011→+A, 0111→+2A-A

Division

Division Algorithms: Restoring (slower, simpler), Non-restoring (faster), SRT (used in Pentium). Takes 10-40 cycles.

Floating-Point

Floating-Point Unit (FPU): Separate hardware for IEEE 754. Handles normalization, rounding, special values (NaN, Infinity). Much slower than integer ops.

Common Use Cases

  • Programming: Low-level operations, bit manipulation, flags, permissions
  • Networking: IP addresses, subnet masks, packet analysis
  • Graphics: Color manipulation (RGB values), image processing
  • Cryptography: Encryption algorithms, hash functions
  • Embedded Systems: Hardware register manipulation, I/O control
  • Data Compression: Huffman coding, run-length encoding
  • Game Development: Collision detection, state management
  • Database: Bitwise indexes, flag storage optimization

Conversion Examples

Binary to Decimal: 11010₂ = 1×16 + 1×8 + 0×4 + 1×2 + 0×1 = 26₁₀
Decimal to Binary: 26₁₀ = 16 + 8 + 2 = 11010₂
Hexadecimal to Decimal: 1A₁₆ = 1×16 + 10 = 26₁₀
Binary to Hexadecimal: 11010₂ = 1A₁₆ (group 4 bits)
Octal to Binary: 32₈ = 011010₂ (3 bits per digit)
Decimal to Hexadecimal: 255₁₀ = FF₁₆

Best Practices

  • Understand bit positions and powers of 2 for quick mental conversions
  • Use hexadecimal for compact binary representation (4 bits per digit)
  • Remember: left shift multiplies by 2, right shift divides by 2
  • Use AND for extracting specific bits (masking)
  • Use OR for setting specific bits to 1
  • Use XOR for toggling bits or simple encryption
  • Be aware of overflow in bitwise operations with large numbers
  • Test edge cases: 0, maximum values, negative numbers

Performance Considerations

Bitwise operations are among the fastest CPU operations, often completing in a single clock cycle. They're used for optimization in performance-critical code. Shifting is faster than multiplication/division by powers of 2. Bit manipulation can reduce memory usage by packing multiple boolean flags into a single integer.

Real-World Applications

File Permissions (Unix): rwxr-xr-x = 755₈ uses octal for permission bits

IP Subnetting: 255.255.255.0 = /24 uses binary for network masks

RGB Colors: #FF5733 uses hex for red, green, blue values

Bit Flags: enum { READ=1, WRITE=2, EXECUTE=4 } allows combining permissions

Checksum Calculation: XOR operations for error detection

UUID Generation: Hex representation of 128-bit identifiers

Quick Tips

💡 Binary: Count from right, each position doubles (1, 2, 4, 8, 16...)

💡 Hex: F = 15, A = 10. Each hex digit = 4 binary bits

💡 Octal: Each digit = 3 binary bits. Used in file permissions

💡 Powers of 2: 2⁸=256, 2¹⁰=1024 (1K), 2¹⁶=65536 (64K)

💡 Negative binary: Two's complement = invert bits and add 1

💡 Quick check: If last bit is 1, number is odd

Frequently Asked Questions

Why do programmers use hexadecimal?

Hexadecimal provides a compact way to represent binary data. Each hex digit represents exactly 4 binary bits, making it easier to read and write than long binary strings. It's widely used in memory addresses, color codes, and debugging.

How do bitwise operations work?

Bitwise operations work on individual bits of numbers. AND (&) outputs 1 only if both bits are 1. OR (|) outputs 1 if at least one bit is 1. XOR (^) outputs 1 if bits are different. NOT (~) inverts all bits. Shift operations move bits left or right.

What's the difference between >> and >>>?

The >> operator is arithmetic right shift which preserves the sign bit (fills with sign bit). The >>> operator is logical/unsigned right shift which always fills with 0. This matters for negative numbers in two's complement representation.

How to convert between binary and hexadecimal quickly?

Group binary digits in sets of 4 from right to left. Each group of 4 bits equals one hexadecimal digit. Example: 11010110₂ = 1101 0110 = D6₁₆. This works because 16 = 2⁴.

What are practical uses of XOR?

XOR is used for: toggling bits, swapping variables without temp storage, simple encryption, checksum calculations, finding unique elements, and detecting bit differences. It's a key operation in many algorithms.

Why is octal still used?

Octal is primarily used in Unix/Linux file permissions (read=4, write=2, execute=1). It provides a compact representation where each digit represents 3 bits, making it natural for representing permission triplets (user, group, others).

How do I handle negative numbers in binary?

Most systems use two's complement: invert all bits and add 1. Example: -5 in 8-bit = ~00000101 + 1 = 11111010 + 1 = 11111011. The leftmost bit indicates sign (1=negative).

What's the maximum value in different systems?

For n bits: Binary max = 2ⁿ-1 (unsigned). 8-bit = 255, 16-bit = 65535, 32-bit = 4294967295. Signed uses one bit for sign, so max = 2ⁿ⁻¹-1. Example: signed 8-bit max = 127.

Related Tools