Skip to main content
CheckTown
Converters

Binary Calculator: Bitwise Operations Explained

Published 5 min read
In this article

What Is Binary Arithmetic?

Binary arithmetic is math performed in base-2, using only the digits 0 and 1. Every digital computer processes data as binary at the hardware level -- integers, floating-point numbers, memory addresses, and even text are ultimately stored and manipulated as sequences of bits. Understanding binary is essential for low-level programming, networking, and digital logic.

Beyond simple addition and subtraction, binary arithmetic includes bitwise operations: AND, OR, XOR, NOT, and bit shifts. These operations work on individual bits and are fundamental to tasks like setting permission flags, computing checksums, masking IP addresses, and optimizing performance-critical code.

How Bitwise Operations Work

Bitwise operators process integers bit by bit. Each operation has a truth table that defines the output for every combination of input bits.

  • AND, OR, XOR -- AND (&) returns 1 only when both bits are 1, OR (|) returns 1 when either bit is 1, and XOR (^) returns 1 when the bits differ. These are used for masking, combining flags, and toggling bits
  • NOT and shifts -- NOT (~) flips all bits, left shift (<<) multiplies by powers of 2, and right shift (>>) divides by powers of 2. Arithmetic right shift preserves the sign bit for negative numbers
  • Two's complement -- modern computers represent negative integers using two's complement, where -1 is all 1-bits. This lets the same hardware perform both addition and subtraction without separate circuitry

Try it free — no signup required

Calculate Binary Operations →

When To Use Bitwise Operations

Bitwise operations are used in systems programming, networking, and performance-sensitive applications.

  • Permission flags -- Unix file permissions (rwxr-xr-x = 0755) use bit fields where each bit represents a specific permission, checked with AND and set with OR
  • Subnet masks -- network engineers use AND operations between IP addresses and subnet masks to determine network boundaries and route traffic correctly
  • Graphics and color manipulation -- pixel colors stored as 0xRRGGBB integers use bit shifts and masks to extract or modify individual red, green, and blue channels

Frequently Asked Questions

What is the difference between signed and unsigned integers?

Unsigned integers use all bits for the magnitude, so an 8-bit unsigned integer ranges from 0 to 255. Signed integers reserve the highest bit for the sign (positive or negative), so an 8-bit signed integer ranges from -128 to 127 using two's complement. The choice affects how right shift and comparison operations behave.

Is bit shifting the same as multiplying or dividing by 2?

Left shifting by n positions is equivalent to multiplying by 2^n for non-negative integers. Right shifting divides by 2^n, but the behavior for negative numbers depends on whether it is an arithmetic shift (preserves sign) or a logical shift (fills with zeros). In most languages, >> is arithmetic for signed types and >>> (where available) is logical.

Why are bitwise operations still relevant in modern code?

Bitwise operations are used extensively in feature flags, permission systems, network protocols, cryptography, hash functions, graphics rendering, and data compression. They execute in a single CPU cycle and use no extra memory, making them ideal for performance-critical paths. Modern frameworks also use bitmasks internally -- React uses them for effect flags, and Linux uses them for file permissions.

Related Tools