Bits and Number Systems Explained
A bit is the atomic unit of digital information, representing a single logical value: 1 or 0. Computers process information by grouping bits into larger structures—a byte contains 8 bits, allowing 28 = 256 distinct states.
Most programming involves three number systems:
- Binary (base 2): Digits are 0 and 1. Used directly by processors. Example: 10102 = 1010
- Decimal (base 10): Familiar everyday system. Example: 8710
- Octal (base 8): Digits 0–7, historically important in Unix file permissions. Example: 1278 = 8710
Converting between systems: 87 in decimal becomes 1010111 in binary and 127 in octal.
Understanding Bitwise Operators
Bitwise operators compare two numbers position-by-position in binary, generating a result based on logical rules applied to each bit pair.
Bitwise AND: Returns 1 only when both input bits are 1; otherwise returns 0. Useful for masking—extracting specific bits.
Bitwise OR: Returns 1 if either input bit is 1. Combines flags and sets multiple bits simultaneously.
Bitwise XOR (exclusive OR): Returns 1 only when bits differ. Toggles flags and detects changes between states.
Example with 87 (binary: 1010111) AND 101 (binary: 1100101):
- Position 0: 1 AND 1 = 1
- Position 1: 1 AND 0 = 0
- Position 2: 1 AND 1 = 1
- Result: 1010101 = 85 in decimal
Bitwise Operation Formulas
These are the core logical rules applied at each bit position. The calculator supports 8, 16, and 20-bit representations:
AND: result = input1 ∧ input2
OR: result = input1 ∨ input2
XOR: result = input1 ⊕ input2
input1, input2— Numbers in binary, octal, or decimal format to be compared bit-by-bitresult— Output number displayed in binary, octal, and decimal after the operation
How to Use This Calculator
Select your bit width (8, 16, or 20 bits) to define the range of valid inputs—8 bits supports −128 to 127 (signed) or 0 to 255 (unsigned).
Choose your input datatype: binary (prefix with 0b or use 0 and 1), octal (prefix with 0o or use 0–7), or decimal. Enter two numbers and select your operation (AND, OR, or XOR).
The calculator immediately displays the result in all three numeral systems. Check the sign handling if using negative numbers—two's complement representation is applied automatically for signed datatypes.
Common Pitfalls and Tips
Bitwise operations are precise, but input errors and sign misunderstandings are frequent.
- Watch Your Number System — A number entered as '101' in decimal is very different from 101 in binary (5 in decimal). Always explicitly specify or verify your input base to avoid off-by-one errors and incorrect results.
- Bit Width Affects Range — Selecting 8 bits limits unsigned integers to 0–255, while 16 bits extends to 0–65535. Oversized inputs will trigger an error. Plan your bit width before entering large numbers.
- Signed vs. Unsigned Matters — Negative numbers use two's complement in signed mode, which inverts bits and adds 1. This changes XOR and AND results dramatically. Confirm your datatype selection matches your use case.
- XOR for Toggle, AND for Masking — Use XOR to flip specific bits and detect changes. Use AND to isolate bits by zeroing unwanted positions. Mixing operators often produces unexpected results; test with small examples first.