Quant Memo
Coding/●●●●●

Strip and isolate the lowest set bit for every number up to n

For every integer i from 1 to n, report two things: the value after clearing its lowest set bit (i & (i - 1)) and the value of the lowest set bit alone (i & -i). Also flag which i are exact powers of two.

n = 6
i=1 (001): cleared 0, lowest 1, power-of-two? yes
i=2 (010): cleared 0, lowest 2, yes
i=3 (011): cleared 2, lowest 1, no
i=4 (100): cleared 0, lowest 4, yes
i=5 (101): cleared 4, lowest 1, no
i=6 (110): cleared 4, lowest 2, no

Return the two tables and the power-of-two flags.

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions