Time/Numbers

Bitwise Operations

AND/OR/XOR/NAND/NOR/XNOR/NOT and shifts on integers of a given width (8/16/32/64 bits) in any number base.

Bitwise operations work directly on a number's individual bits rather than its value as a whole — the basis for permission flags, network protocols, and low-level optimization.

How to use it

Common uses

Things to keep in mind

The result of NOT and shifts depends on the chosen bit width — the same number gives a different result at 8-bit versus 32-bit width because of the different number of bits involved.

XOR is often used to toggle a flag — applying XOR twice with the same value returns the original.

Article about this tool: Bitwise operations: how AND, OR, and XOR work at the bit level

Frequently asked questions

What's the difference between AND, OR, XOR, NAND, NOR, XNOR, and NOT?

AND gives 1 only when both bits are 1, OR gives 1 when at least one is 1, and XOR gives 1 when the bits differ. NAND/NOR/XNOR are their negations, and NOT inverts every bit of a single operand.

How is the bit width and sign of a number handled?

Choose a width of 8/16/32/64 bits — operations and shifts happen within that range. Negative numbers are represented in two's complement, so the result of NOT or a shift can look unexpected if you're used to decimal notation.

What's the difference between a left shift and a right shift?

A left shift (<<) adds zeros on the right, effectively multiplying by 2 raised to the shift amount. A right shift can be logical (adds zeros on the left) or arithmetic (preserves the sign bit) — the type depends on the selected mode.

Why does "if (a & b == c)" often not work as expected?

In most languages, the comparison operator == has higher precedence than the bitwise & operator, so the expression evaluates as "a & (b == c)" rather than what the author usually intends. Always wrap bitwise operations in explicit parentheses to avoid this trap.

Why use bitwise flags instead of separate boolean variables?

A set of flags packed into a single number is more compact to store and transmit (e.g. in network protocols or file formats), and checking or changing a specific flag with AND/OR/XOR is a single fast operation.

Articles: Time/Numbers

Unix time: why computers count time from 1970

Why January 1, 1970 became the reference point for time in most computer systems.

Cron expressions: how to decode a task schedule

How to decode an expression like */15 * * * * and know exactly when the job will run.

Number systems: why binary, octal, and hexadecimal exist

Why the color #FF5733 in CSS is written in hexadecimal digits instead of decimal.

Date difference: why counting days is trickier than it looks

Why naively subtracting dates can give the wrong result because of leap years.

Age calculation: why it's not just subtracting years

Why simply subtracting birth years can give an age one year higher than the real one.

Time zones: why UTC isn't the same thing as GMT

Why UTC never changes twice a year, while London time (GMT/BST) does.

Duration calculator: how to correctly add up hours, minutes, and seconds

Why adding minutes and seconds requires "carrying" the remainder to the next place, just like in decimal addition.

ISO 8601 duration: how to write a duration in a standard format

Why PT1H30M means "1 hour 30 minutes," not "1 minute 30 hours."

Week number: why different countries count weeks differently

Why January 1 sometimes falls into week 52 or 53 of the previous year under the ISO 8601 standard.

Roman numerals: how a system with no zero and no place value works

Why IV means 4, not 6, and how the subtraction rule works in Roman numerals.

IEEE 754: why 0.1 + 0.2 doesn't equal 0.3 in code

Why almost every programming language prints 0.1 + 0.2 as 0.30000000000000004.

Number to words: why turn digits into text

Why bank documents write an amount both in digits and in words at the same time.