8. Clear all bits from the LSB #

Created Tuesday 30 June 2020

Given n(base 2 number) and i(integer), unset all bits on the right of i, incl.

x =   10100; // i=3
2^(i+1) = 1000; // i = 3
// we need  11000
// 2(i+1)-1 00111
// flip it, ~
return x & ~((1<<(i+1))-1));

Clear all bits from MSB e.g 10101, i = 2. Ans = 10000