Convert & calculate · Bitwise operations

Bitwise calculator

Shift and mask 256-bit numbers with AND, OR, XOR, NOT and bit shifts.

Runs in your browser

The calculation

Values are read as hexadecimal. Everything recalculates as you type.

With or without 0x: ff and 0xff both mean 255.

A bit is 1 in the answer only when it is 1 in both values. This is how you mask a field out of a packed number.

Bits set to 1 here survive; everything else is cleared. 0xff keeps the last byte.

The answer shows up here

Enter a value on the left and you'll get the result in decimal, hexadecimal and binary at the same time.

Every operation, in one line each

The same six operators Solidity gives you.

AND (&)

A bit is 1 in the answer only when it is 1 in both values. This is how you mask a field out of a packed number.

OR (|)

A bit is 1 in the answer when it is 1 in either value. This is how you switch a flag on.

XOR (^)

A bit is 1 in the answer when the two values disagree. XOR with the same value twice gets you back where you started.

NOT (~)

Every 1 becomes 0 and every 0 becomes 1. On whole numbers that is the same as -(x + 1), so the answer comes out negative.

SHL (<<)

Moves every bit left, padding with zeros. Shifting left by n multiplies by 2 to the power n.

SHR (>>)

Moves every bit right, dropping what falls off the end. Shifting right by n divides by 2 to the power n and rounds down.

About this tool

A bitwise calculator applies AND, OR, XOR, NOT and left or right shifts to a number one bit at a time. Enter values in hex, up to the EVM's 256-bit word size and beyond, and see the result in hexadecimal, decimal and binary at once. Use it to build bitmasks, pull a field out of a packed uint256, or check flag logic before you write it in Solidity.

Common questions

How do I extract bits from a packed uint256?

Shift the value right until the field you want sits in the lowest bits, then AND it with a mask as wide as the field. For an 8-bit field that starts at bit 16, shift right by 16 (enter 10, since shifts are in hex) and AND with 0xff.

Why does NOT give a negative number?

The calculator works on plain whole numbers, where flipping every bit of x gives -(x + 1). Solidity's ~ on a uint256 gives 2^256 - 1 - x instead. To get that, AND the negative result with 0x followed by 64 f characters.

Does a left shift wrap at 256 bits like the EVM?

No. The result keeps every bit, so shifting a large value left can grow past 256 bits. The EVM drops anything above bit 255, so AND the result with 0x followed by 64 f characters to see what a contract would store.