Toggle All Bits After MSB (Including MSB)

E · easyP · Verified PYQmath

Given positive integer N, convert to binary and toggle ALL bits (including MSB). Print resulting integer.

1<=N<=100
Input
10
Output
5
k = (1 << (floor(log2(N))+1)) - 1. Answer = N XOR k.
10 = 1010 binary. Toggle all 4 bits = 0101 = 5.
← Supermarket Scanner — Product of All DigitsToggle Bits — Number of Bits to Flip →
Report an issue with this question