Learn binary, hex and bitwise operations
Each guide answers one question, with a worked example that follows the value in its editor.
Concepts
How binary, two’s complement, masks and byte order work.
- How binary numbers work
Binary uses two digits, 0 and 1.
- How two’s complement works
Two’s complement stores a negative value in a fixed number of bits.
- Signed and unsigned values
An unsigned value uses all its bits for the magnitude.
- How to use a bit mask
A mask is a value that selects one or more bits.
- How bit shifts work
A left shift moves all bits to a higher position.
- Big endian and little endian
Endianness sets the order of the bytes of a value in memory.
- Why hex suits binary values
One hex digit shows exactly four bits.
- How IEEE 754 stores a float
A 32-bit float holds three fields: one sign bit, an 8-bit exponent and a 23-bit fraction.
- How to count the bits with the value 1
The population count of a value is the number of bits that have the value 1.
- The pitfalls of register code
Register code fails in a small number of ways, over and over.
- Bit positions and off-by-one errors
Bit 0 is the rightmost bit, and it holds the value 1.
- The read, change and write race
The idiom reg |= mask is three operations: a read, a change and a write.
- The traps of a shift in C
The literal 1 is a signed int, so 1 << 31 overflows it and the standard leaves the result undefined.
Converters
Convert between decimal, hex, octal and binary, with every step.
- Convert decimal to binary
To convert a decimal value into binary, divide the value by 2 and keep the remainder.
- Convert binary to decimal
To convert a binary value into decimal, find each bit that has the value 1.
- Convert decimal to hex
To convert a decimal value into hex, divide the value by 16 and keep the remainder.
- Convert hex to decimal
To convert a hex value into decimal, take each digit and its position.
- Convert binary to hex
To convert a binary value into hex, split the bits into groups of four.
- Convert hex to binary
To convert a hex value into binary, take each hex digit in turn.
- Convert decimal to octal
Octal uses the eight digits 0 to 7.
- Convert octal to decimal
Each octal position holds a power of eight.
- Convert octal to binary
One octal digit holds exactly three bits.
- Convert a float to binary
A 32-bit float holds a sign bit, an 8-bit exponent and a 23-bit fraction.
- Swap the bytes of a value
A byte swap reverses the order of the bytes of a value.
Operations
AND, OR, XOR, NOT, the negated forms and the two shifts, with truth tables.
- Bitwise AND
The AND operation compares the two values bit by bit.
- Bitwise OR
The OR operation compares the two values bit by bit.
- Bitwise XOR
The XOR operation compares the two values bit by bit.
- Bitwise NOT
The NOT operation inverts every bit of one value.
- Left shift
A left shift moves every bit to a higher position.
- Right shift
A right shift moves every bit to a lower position.
- Bitwise NAND
NAND is an AND with the result inverted.
- Bitwise NOR
NOR is an OR with the result inverted.
- Bitwise XNOR
XNOR is an XOR with the result inverted.
- Binary addition
Binary addition works one position at a time, from the right.
- Two’s complement calculator
Two’s complement holds a negative value in a fixed number of bits.
- Bit mask calculator
A bit mask is a value that selects the bits you act on.
- Binary subtraction
Binary subtraction works one position at a time, from the right.
- Binary multiplication
Binary multiplication adds a shifted copy of the first value for each bit of the second.
- Binary division
Binary long division compares the divisor against the value from the left.
Text and characters
Read a byte as a character, and write text as bits.
- Convert text to binary
A computer holds text as numbers.
- Convert binary to text
Text in a binary stream is a sequence of character codes.
- ASCII table
ASCII maps the codes 0 to 127 to characters.
Serial and registers
Plot live serial data and read a hardware register bit by bit.
- Serial plotter in the browser
Beetwise plots live serial data in a browser tab.
- UART debugger in the browser
Beetwise reads a UART stream and shows each value as a register.
- Register viewer
A register viewer shows every bit of a value at the same time.
- How to read a microcontroller register
A microcontroller register packs several fields into one value.
- STM32 GPIO MODER calculator
GPIOx_MODER holds two bits for each of the 16 pins of a port.
- AVR register calculator
An AVR register is 8 bits wide, and each bit has a name in the datasheet.
- ESP32 GPIO register calculator
An ESP32 GPIO register holds one bit for each pin, so bit n belongs to GPIO n.
Reference
Tables to look up: integer ranges, and the operators of each language.
- Integer ranges by bit width
A register of n bits holds 2^n different values.
- Bitwise operators in C and C++
C has six bitwise operators: AND, OR, XOR, NOT and the two shifts.
- Bitwise operators in Python
Python has the same six bitwise operators as C.