🏆 US-Registered Digital Marketing Agency
Advertisement
Advertisement
DEVELOPER

Bitwise Calculator — AND, OR, XOR, NOT and shifts

Apply bitwise operators to two integers and read the result in binary, hexadecimal, decimal and octal at once.

A leading 0x, 0b or 0o is accepted and overrides the selector. Spaces and underscores are ignored.
This changes what the decimal readings mean and which right shift is the natural one. The bit patterns produced by AND, OR, XOR and NOT are identical either way.
Bit by bit:
Result in binary
0
 
0
Decimal
0
Hexadecimal
0
Octal
0
Set bits (popcount)
Tip: the same bit pattern reads as two different decimal numbers depending on the width and the signed setting. Both are shown so the ambiguity is visible.
Advertisement

The bitwise calculator above applies AND, OR, XOR, NAND, NOR, NOT and the full family of shifts and rotations to two integers, and shows the answer in binary, hexadecimal, decimal and octal together with the operands lined up bit by bit. Word width and signed interpretation are both explicit, because those two settings are where almost all real confusion about bitwise operations lives.

Arb Digital publishes this as one of a set of free developer tools. It shows the operands stacked above the result on purpose: a bitwise operation is a column of independent single-bit decisions, and once you can see the columns, AND, OR and XOR stop being abstract. Nothing carries, nothing borrows, and column seven has no idea what column six is doing.

What This Bitwise Calculator Does

It parses each operand in the base you choose, fits both into a word of the width you choose, applies the operator, and reports the result four ways at once. It also counts the set bits in the result, which is the number a surprising amount of low-level code actually wants — flags enabled, subnet size, hamming weight.

The boundaries against the neighbouring tools are clean. The number base converter converts one value between bases; it does not operate on two of them, and it has no concept of word width. The binary arithmetic calculator adds, subtracts, multiplies and divides, where columns interact through carries and borrows. This page is the opposite case: purely positional logic, no interaction between columns, except in the shift and rotate operations where bits move sideways as a block.

How to Use It

  1. Enter the operands in whatever base is natural. Hex for masks and flags, decimal for quantities, binary when you want to see the pattern you are typing.
  2. Choose the operation. Two-operand logic uses both fields; NOT and the shifts use A and the shift amount.
  3. Set the word width to match your type. This decides how many bits exist, and therefore what NOT and the shifts produce.
  4. Set signed or unsigned. It changes the decimal readings and determines whether a right shift should be arithmetic or logical.
  5. Read the bit-by-bit panel to see which columns contributed, rather than trusting the decimal number alone.

The Operators and How They Work

Every two-operand bitwise operator is a truth table applied independently to each bit position. AND gives 1 only when both bits are 1, which makes it the masking operator: x & 0x0F keeps the low nibble and clears everything else. OR gives 1 when either bit is 1, which sets bits: x | 0x80 turns on the top bit of a byte. XOR gives 1 when the bits differ, which toggles bits and, usefully, is its own inverse — applying the same XOR twice returns the original value. NOT inverts every bit, and is the one operator whose result depends entirely on how wide you said the word was.

A worked example with the defaults. A is 0xB5, which is 10110101 in binary and 181 in decimal. B is 0x6E, which is 01101110 and 110. Lining them up, AND gives 00100100 — the two columns where both operands hold a 1 — which is 0x24, 36 decimal, and has a popcount of 2. The same pair under OR gives 11111111 (255) and under XOR gives 11011011 (0xDB, 219). Mozilla's documentation for the bitwise AND operator sets out the same truth table and notes a detail worth knowing in JavaScript specifically: for ordinary numbers the operator converts both operands to 32-bit integers, so bits above the 32nd are discarded.

Advertisement

Signed Versus Unsigned Is the Whole Problem

Here is the thing worth taking away. The bit pattern is the only thing that exists in memory; signedness is a claim you make about how to read it. In eight bits, 11111111 is 255 unsigned and −1 signed. AND, OR, XOR and NOT do not care at all — they produce the same bits either way — so the operation is unambiguous and only the decimal reading changes.

Right shift is the exception, and it is a real fork in behaviour rather than a display difference. A logical right shift feeds zeros in at the top. An arithmetic right shift replicates the sign bit, so a negative number stays negative. Shift 11111111 right by one: logically you get 01111111 (127), arithmetically you get 11111111 (still −1). Both are correct for their intended reading, and picking the wrong one turns a small negative number into a very large positive one. Different languages resolve this differently — some choose by the operand's type, JavaScript gives you separate >> and >>> operators, and in C the behaviour of right-shifting a negative signed value is defined by the standard rather than by intuition, which is one reason the ISO/IEC 9899 working draft N1570 is worth having open. Our two's complement converter goes further into the representation itself.

Word Width Changes the Answer, Especially for NOT

NOT is the operator that exposes width most brutally. Invert 00000101 in eight bits and you get 11111010, which is 250 unsigned or −6 signed. Invert the same value in 32 bits and you get 11111111111111111111111111111010, which is 4,294,967,290 unsigned — and still −6 signed. The signed reading is stable across widths; the unsigned one is not, and it changes by a factor of sixteen million.

Shifts are width-sensitive in the other direction. Left-shifting pushes bits off the top of the word, and they are gone. Shift 10110101 left by 3 in eight bits and the three high bits vanish, leaving 10101000. Do it in 16 bits and nothing is lost, giving 0000010110101000. Rotation exists precisely because sometimes you want those bits back: a rotate wraps them around to the other end rather than discarding them, which is why cryptographic and hashing code uses rotations rather than shifts. Set the width selector to something different and watch the same operation produce a different answer — that is not a bug, it is the point.

What Bitwise Operators Are Actually For

Four patterns cover most real use. Flags in a bit field: OR to set, AND with the complement to clear, AND to test, XOR to toggle. A single integer can carry 32 independent booleans, which is why permission bits, feature flags and hardware registers are packed this way. Masking: AND against a mask isolates a field inside a packed value, which is how colour channels are pulled out of a 32-bit pixel and how a network prefix is extracted from an address — our IP subnet calculator is doing exactly this behind the scenes.

Cheap arithmetic: shifting left by n multiplies by 2ⁿ and shifting right divides, though the second one is not equivalent to division for negative values under every rounding rule, so it is a trap as often as a trick. Modern compilers do this transformation for you, and hand-written shift tricks mostly cost readability now. Set membership and counting: a bitmask is a set, AND is intersection, OR is union, XOR is symmetric difference, and popcount is the cardinality. The logic gate calculator and the truth table generator cover the same logic at the gate and expression level.

The Traps Worth Knowing About

Three catch people repeatedly. First, operator precedence: in C and the languages that copied it, & and | bind more loosely than the comparison operators, so x & 1 == 0 parses as x & (1 == 0) and does not do what it looks like. Parenthesise everything. Second, shifting by more than the width is undefined behaviour in C and wraps modulo the width in JavaScript, so the same expression gives different answers in different languages rather than an error.

Third, silent promotion: in many languages small integer types are promoted before the operation runs, so a NOT applied to an 8-bit value may be evaluated in 32 bits and produce a much larger number than expected when assigned back. This tool makes width explicit so you can see what each choice produces, but your language will make its own decision — check what it promotes to before relying on the result.

Building something where these details matter?

Arb Digital builds web applications and the systems behind them, and we care about the parts that fail quietly. See what we do, or tell us what you are working on.

See Web Design Services Talk to Arb Digital

Common Mistakes to Avoid

  • Ignoring word width on NOT — inverting a value in 8 bits and in 32 bits gives wildly different unsigned results from identical input.
  • Using the wrong right shift — logical fills with zeros, arithmetic replicates the sign bit, and choosing wrongly turns −1 into 2,147,483,647.
  • Forgetting operator precedencex & 1 == 0 is not the test you meant in C-like languages. Parenthesise.
  • Shifting by the width or more — undefined in C, modulo the width in JavaScript, and rarely what you intended in either.
  • Confusing bitwise with logical operators& is not &&, and the difference includes short-circuit evaluation as well as the result.

Related Free Tools From Arb Digital

Use the number base converter to convert a single value, the binary arithmetic calculator for sums with carries and borrows, the two's complement converter for signed representation, the IP subnet calculator for masking in practice and the truth table generator for boolean expressions. The full free online tools hub lists every calculator we publish.

Frequently Asked Questions

What does the bitwise AND operator do?

It compares two values bit by bit and produces a 1 only where both operands have a 1. With A as 10110101 and B as 01101110 the result is 00100100. It is the standard way to mask a value, keeping the bits the mask has set and clearing the rest.

What is the difference between a logical and an arithmetic right shift?

A logical shift feeds zeros in at the top; an arithmetic shift replicates the sign bit so negative values stay negative. Shifting 11111111 right by one gives 01111111 logically and 11111111 arithmetically — 127 versus −1 on the same input.

Does signed or unsigned change the result of AND, OR and XOR?

No. Those operators work on the bit patterns and produce identical bits either way. What changes is the decimal reading of the result, and which right shift is the appropriate one to use.

Why does NOT give a different answer at different word widths?

Because NOT inverts every bit that exists, and the width decides how many that is. Inverting 00000101 gives 250 in 8 bits and 4,294,967,290 in 32 bits unsigned, though both read as −6 when interpreted as signed.

How do I set, clear, test and toggle a single bit?

Set with OR against a mask with that bit on, clear with AND against the complement of that mask, test with AND and check for a non-zero result, and toggle with XOR against the mask. These four cover most bit-field manipulation.

What is the difference between a shift and a rotate?

A shift discards the bits that fall off the end and feeds new bits in at the other side. A rotate wraps them around, so no information is lost. Cryptographic and hashing algorithms use rotations for exactly that reason.

Why is x & 1 == 0 not testing the low bit?

Because in C and languages that copied its precedence, the equality operator binds tighter than the bitwise AND, so the expression parses as x & (1 == 0). Write (x & 1) == 0 instead. This is one of the most persistent bitwise bugs there is.

This tool demonstrates fixed-width bitwise operations. Integer promotion, shift behaviour and signed conversion are language and platform specific, and should be confirmed against your language standard.

Advertisement
Advertisement

Take it further