C++ bit test
(x >> i) & 1 == 1: x.bit(i) reads one bit
let n: 1..=20 = read()
let x: Int = read()
for i in 0..n:
if (x >> i) & 1 == 1:
print(i)let n: 1..=20 = read()
let x: Int = read()
for i in 0..n:
if x.bit(i):
print(i)Why
C++ tests bit i of x with (x >> i) & 1 or x & (1 << i), compared against 1 or 0. CP Lang names the operation: x.bit(i) is true when bit i is set, for every i (bits past 63 read the sign), so the shift, the mask and the comparison collapse into one call that says what it tests. The fix rewrites the comparison to x.bit(i), or not x.bit(i) when it tests for a clear bit. A subset loop over 0..(1 << n) usually also wants std.bits subset iteration.
The practice it serves
A hand-written pattern the language or the standard library already says in one step.
Not flagged
A near miss: close to the pattern, but not it.
let n: 1..=20 = read()
let x: Int = read()
for i in 0..n:
if (x >> i) & 3 == 1:
print(i)Turn it off
For one block, wrap it in with lint(manual_bit_test: allow):. For a project, add manual_bit_test = "allow" under [lints] in cp.toml.
Generated from spec/lints/manual_bit_test, the same record the linter is built from.