To divide by 3, I used a multiplication trick. On tiny devices, division is often implemented as a (slow) library call, but if you’re dividing by a constant, you can usually find a good equivalent fixed-point constant. “One third” in binary is “0.5555…” repeating, so using a 16-bit fixed-point fraction, you can multiply by 0x5556 and shift right 16 bits.

Because shifting right by 16 is the math equivalent of “floor” instead of “round”, so we need the result to be not just “as close to 1/3 as possible”, but the error needs to be on the high side instead of the low side.