blob: d2e15154d898f71ed966c0e0422d3ce933552221 (
plain)
1
2
3
4
5
6
7
8
9
10
11
|
def set_middle_bits(n):
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
return (n >> 1) ^ 1
def toggle_middle_bits(n):
if (n == 1):
return 1
return n ^ set_middle_bits(n)
|