Skip to content

Programmer math: hex, binary and bitwise operations

Use hex, binary and octal literals in Zlv Desktop, convert with in hex or in bin, shift bits, and apply AND, OR, XOR and NOT with a configurable word size.

Zlv Desktop includes a programmer's toolkit: number bases, bit shifts, bitwise logic and bit helpers. These features use the desktop Rust engine. In the web app they need the Rust WebAssembly engine.

Number literals

Prefix a number with 0x for hexadecimal, 0b for binary or 0o for octal. Results are shown in decimal unless you ask for a base.

Desktop example
Uses the native Rust engine: copy it into Zlv Desktop to see results.

Results appear next to each line in Zlv Desktop. Download it free.

Converting between bases

Add in hex, in bin, in oct or in dec to any result. in sci gives scientific notation.

Desktop example
Uses the native Rust engine: copy it into Zlv Desktop to see results.

Results appear next to each line in Zlv Desktop. Download it free.

to_base(n, base) and from_base("digits", base) handle any base, and from_hex("ff") reads a hex string.

Desktop example
Uses the native Rust engine: copy it into Zlv Desktop to see results.

Results appear next to each line in Zlv Desktop. Download it free.

Shifts

Desktop example
Uses the native Rust engine: copy it into Zlv Desktop to see results.

Results appear next to each line in Zlv Desktop. Download it free.

Bitwise logic

Use the words AND, OR, XOR, NOT, NAND, NOR and XNOR (upper or lower case), or the symbols &, | and ~.

Desktop example
Uses the native Rust engine: copy it into Zlv Desktop to see results.

Results appear next to each line in Zlv Desktop. Download it free.

Word size and unsigned values

NOT and the bit helpers work within a word size, 32 bits by default. That is why NOT 0 in hex shows 0xFFFFFFFF. Change it to 8, 16, 32 or 64 bits in Settings > Calculation > Bitwise word size. Add in uint to read a negative result as an unsigned number.

Desktop example
Uses the native Rust engine: copy it into Zlv Desktop to see results.

Results appear next to each line in Zlv Desktop. Download it free.

Bit helpers

FunctionResult
popcount(n)Number of 1 bits
clz(n) / ctz(n)Leading / trailing zero bits
set_bit(n, k), clear_bit(n, k), toggle_bit(n, k)Change bit k
test_bit(n, k)1 if bit k is set
mask(k)A mask of k low bits
rol(n, k) / ror(n, k)Rotate left / right
parity(n)1 if the number of set bits is odd
bswap(n)Reverse byte order
Desktop example
Uses the native Rust engine: copy it into Zlv Desktop to see results.

Results appear next to each line in Zlv Desktop. Download it free.

Data sizes

Data size units such as KiB, MiB and GB are covered in Unit conversions.

All guides