The snippet library.
Small, practical examples. Search by task or narrow things down below.
Assemble a big-endian 16-bit value
Arduino / C: Cast to an unsigned 16-bit type before shifting the high byte.
Arduino / C✓ Tested
Assign a bit from a Boolean state
Arduino / C: bitWrite accepts a bit position and a zero or nonzero value.
Arduino / C✓ Tested
Calculate a CRC-8 over a byte sequence
Arduino / C: This implementation uses polynomial 0x07, initial zero, and no reflection.
Arduino / C✓ Tested
Calculate a simple packet XOR checksum
Arduino / C: XOR all payload bytes to produce a basic error-detection byte.
Arduino / C✓ Tested
Clear a flag without affecting neighboring bits
Arduino / C: bitClear resets only the selected bit.
Arduino / C✓ Tested
Encode a value as little-endian bytes
Arduino / C: Store the least significant byte first when the protocol requires it.
Arduino / C✓ Tested
Extract the low and high byte of a value
Arduino / C: lowByte and highByte expose the two bytes of a 16-bit value.
Arduino / C✓ Tested
Pack two small values into one byte
Arduino / C: Mask each four-bit value before combining it.
Arduino / C✓ Tested
Require every bit in a mask
Arduino / C: Masking followed by equality tests whether all required flags are set.
Arduino / C✓ Tested
Toggle a bit with an XOR mask
Arduino / C: XOR flips selected bits and leaves all other bits unchanged.
Arduino / C✓ Tested