Programmer Calculator Guide: Binary, Hex & Bitwise Operations

Programming6 min readJanuary 5, 2024

Master number bases and bitwise operations essential for computer science and programming. Learn binary, hexadecimal, octal systems, and bit manipulation techniques with practical examples.

Why Programmer Calculators Matter

Understanding number bases and bitwise operations is fundamental for programmers, computer science students, and anyone working with low-level programming, embedded systems, cryptography, or computer graphics.

1. Understanding Number Systems

Decimal (Base 10) - Our Everyday System

Uses digits 0-9. Each position represents a power of 10:

Example: 1234₁₀ = (1×10³) + (2×10²) + (3×10¹) + (4×10⁰) = 1000 + 200 + 30 + 4

Binary (Base 2) - Computer Language

Uses only digits 0 and 1. Each position represents a power of 2:

💻 Binary Example:

1101₂ = (1×2³) + (1×2²) + (0×2¹) + (1×2⁰) = 8 + 4 + 0 + 1 = 13₁₀

Binary Conversion Tips

  • Decimal to Binary: Repeatedly divide by 2, read remainders bottom-up
  • Binary to Decimal: Add powers of 2 for each '1' position
  • Quick powers of 2: 2⁰=1, 2¹=2, 2²=4, 2³=8, 2⁴=16, 2⁵=32, 2⁶=64, 2⁷=128, 2⁸=256

Hexadecimal (Base 16) - Programmer's Friend

Uses digits 0-9 and letters A-F (A=10, B=11, C=12, D=13, E=14, F=15):

🔢 Hex Conversion:

2A7₁₆ = (2×16²) + (10×16¹) + (7×16⁰) = 512 + 160 + 7 = 679₁₀
Why hex? One hex digit = exactly 4 binary digits (bits)

Hex to Binary Conversion

Each hex digit converts to exactly 4 binary digits:

  • 0₁₆ = 0000₂, 1₁₆ = 0001₂, 2₁₆ = 0010₂, 3₁₆ = 0011₂
  • 4₁₆ = 0100₂, 5₁₆ = 0101₂, 6₁₆ = 0110₂, 7₁₆ = 0111₂
  • 8₁₆ = 1000₂, 9₁₆ = 1001₂, A₁₆ = 1010₂, B₁₆ = 1011₂
  • C₁₆ = 1100₂, D₁₆ = 1101₂, E₁₆ = 1110₂, F₁₆ = 1111₂

Octal (Base 8) - Less Common but Important

Uses digits 0-7. Each octal digit represents exactly 3 binary digits:

Example: 755₈ = (7×8²) + (5×8¹) + (5×8⁰) = 448 + 40 + 5 = 493₁₀

2. Bitwise Operations

AND Operation (&)

Returns 1 only when both bits are 1:

1101 (13)

& 1010 (10)

------

1000 (8)

AND Applications

  • Masking: Extract specific bits
  • Testing: Check if bit is set
  • Clearing: Set specific bits to 0

OR Operation (|)

Returns 1 when at least one bit is 1:

1101 (13)

| 1010 (10)

------

1111 (15)

OR Applications

  • Setting bits: Turn specific bits on
  • Combining flags: Merge multiple boolean values
  • Default values: Provide fallback bits

XOR Operation (^)

Returns 1 when bits are different:

1101 (13)

^ 1010 (10)

------

0111 (7)

XOR Applications

  • Toggling: Flip specific bits
  • Encryption: Simple cipher operations
  • Parity checking: Error detection
  • Swapping: Exchange values without temp variable

NOT Operation (~)

Inverts all bits (1 becomes 0, 0 becomes 1):

~ 1101 = 0010 (in 4-bit representation)

3. Bit Shifting Operations

Left Shift (<<)

Moves bits to the left, filling with zeros:

⬅️ Left Shift Example:

5 << 2
101₂ << 2 = 10100₂ = 20₁₀
Rule: Left shift by n positions = multiply by 2ⁿ

Right Shift (>>)

Moves bits to the right:

➡️ Right Shift Example:

20 >> 2
10100₂ >> 2 = 101₂ = 5₁₀
Rule: Right shift by n positions = divide by 2ⁿ (integer division)

Arithmetic vs Logical Right Shift

  • Logical (>>>): Always fills with 0s
  • Arithmetic (>>): Preserves sign bit for negative numbers

4. Two's Complement (Negative Numbers)

Understanding Signed Binary

Computers represent negative numbers using two's complement:

  1. Step 1: Write the positive number in binary
  2. Step 2: Invert all bits (NOT operation)
  3. Step 3: Add 1 to the result

➖ Two's Complement Example:

Find -5 in 8-bit two's complement:
1. +5 = 00000101₂
2. Invert: 11111010₂
3. Add 1: 11111011₂ = -5₁₀

Sign Bit

In signed binary, the leftmost bit indicates sign:

  • 0: Positive number
  • 1: Negative number

5. Practical Programming Applications

Bit Flags and Permissions

Use individual bits to represent boolean flags:

🏴 File Permissions Example:

rwx permissions = 111₂ = 7₈

  • r (read) = 100₂ = 4₈
  • w (write) = 010₂ = 2₈
  • x (execute) = 001₂ = 1₈

chmod 755: rwxr-xr-x = 111101101₂

Color Values in Graphics

RGB colors are often represented in hexadecimal:

  • #FF0000: Pure red (255, 0, 0)
  • #00FF00: Pure green (0, 255, 0)
  • #0000FF: Pure blue (0, 0, 255)
  • #FFFFFF: White (255, 255, 255)
  • #000000: Black (0, 0, 0)

Memory Addresses

Memory addresses are typically displayed in hexadecimal:

  • 32-bit systems: 0x00000000 to 0xFFFFFFFF
  • 64-bit systems: 0x0000000000000000 to 0xFFFFFFFFFFFFFFFF

Bit Manipulation Algorithms

Check if Number is Power of 2

A number is a power of 2 if it has exactly one bit set:

Method: n & (n-1) == 0 (for n > 0)

Count Set Bits

Count the number of 1s in binary representation:

// Brian Kernighan's algorithm

while (n) {

count++;

n = n & (n-1); // Remove rightmost set bit

}

Isolate Rightmost Set Bit

Extract the rightmost 1 bit:

Method: n & (-n)

6. Debugging with Programmer Calculators

Common Programming Scenarios

  • Buffer overflow: Check if values exceed bit width limits
  • Bit masking errors: Verify mask values in different bases
  • Signed/unsigned conversion: Understand value changes
  • Endianness issues: Check byte order in hex

Network Programming

  • IP addresses: Convert between dotted decimal and hex
  • Subnet masks: Calculate network ranges
  • Port numbers: Understand 16-bit limits
  • Protocol headers: Parse binary packet data

7. Advanced Topics

Floating Point Representation

IEEE 754 standard for floating point numbers:

  • 32-bit float: 1 sign + 8 exponent + 23 mantissa bits
  • 64-bit double: 1 sign + 11 exponent + 52 mantissa bits

Bitwise Tricks for Optimization

  • Fast multiplication by powers of 2: Use left shift
  • Fast division by powers of 2: Use right shift
  • Fast modulo by powers of 2: Use AND with (2ⁿ - 1)
  • Toggle case of letters: XOR with 0x20

Cryptographic Applications

  • XOR ciphers: Simple encryption/decryption
  • Hash functions: Bit manipulation for mixing
  • Random number generation: Linear feedback shift registers

Common Mistakes and Pitfalls

⚠️ Watch Out For:

  • Confusing logical vs arithmetic right shift
  • Integer overflow in calculations
  • Sign extension in negative numbers
  • Operator precedence in complex expressions
  • Language-specific behavior differences
  • Endianness assumptions across platforms

Practice Problems

Conversion Practice

  1. Convert 255₁₀ to binary and hexadecimal
  2. Convert 1010110₂ to decimal and hexadecimal
  3. Convert 0x2F₁₆ to binary and decimal
  4. Find -15 in 8-bit two's complement

Bitwise Operation Practice

  1. Calculate 0xA5 & 0x3C
  2. Calculate 0xF0 | 0x0F
  3. Calculate 0xFF ^ 0xAA
  4. Calculate 12 << 3 and 96 >> 2

Real-World Code Examples

Setting and Clearing Bits

// Set bit n: number |= (1 << n)

// Clear bit n: number &= ~(1 << n)

// Toggle bit n: number ^= (1 << n)

// Check bit n: (number & (1 << n)) != 0

Fast Absolute Value

// Without branching

int mask = x >> 31; // -1 if negative, 0 if positive

return (x + mask) ^ mask;

Conclusion

Programmer calculators and bit manipulation are essential skills for any serious programmer. Understanding number bases helps you read assembly code, debug low-level issues, and optimize performance-critical code. Bitwise operations provide elegant solutions to many algorithmic problems and are fundamental to systems programming, graphics, and cryptography.

Practice these concepts regularly, especially when working on embedded systems, game development, or any performance-critical applications. The ability to think in binary and manipulate bits efficiently will make you a more versatile and effective programmer.

💻 Practice Bit Operations

Use our programmer calculator to practice number base conversions and bitwise operations.

Open Programmer Calculator