The snippet library.
Small, practical examples. Search by task or narrow things down below.
Calculate a fixed-window moving average
C: For this small window, sum each complete group and skip incomplete prefixes.
C✓ Tested
Calculate a least common multiple using a GCD
C: Divide before multiplying to reduce the risk of intermediate overflow.
C✓ Tested
Calculate a small factorial iteratively
C: Multiply a bounded sequence using an unsigned accumulator.
C✓ Tested
Calculate an average without integer division truncation
C: Accumulate into double and divide by the nonzero number of readings.
C✓ Tested
Calculate the greatest common divisor
C: Euclid’s algorithm repeatedly replaces a pair with the divisor and remainder.
C✓ Tested
Compute a simple XOR checksum
C: XOR all payload bytes into one byte to demonstrate a basic error-detection field.
C✓ Tested
Count set bits in an unsigned integer
C: Clearing the lowest set bit on each iteration counts only one iteration per set bit.
C✓ Tested
Generate a bounded Fibonacci sequence
C: Keep only the two previous values instead of using exponential recursive calls.
C✓ Tested
Sum the decimal digits of an unsigned integer
C: Repeated division and remainder extract digits without converting to text.
C✓ Tested
Test whether an unsigned value is a power of two
C: A nonzero power of two has exactly one set bit.
C✓ Tested