$ Bit Manipulation - Clear Range Of Bits

for getting the ith bit , we have to write this one n & (1 << i ). when we are reading the last bit in the right as 0th bit .

in books of Narasimha book is n & (1 << (i - 1) is given . because he is assuming the last bit in the right as 1st bit.

same thing i got also on geeks for geeks
because consider a example n=15
13=1101 here the first bit is 1
2nd bit is 0
3rd bit is 1
4th bit is 1.
is there any rule to reading the last bit as oth bit or 1st bit ?

Hello,

When you have to find the ith bit from last then you use n&(1<<i-1) only.

In clearing range of bits we are using a fact that we have to shift each bit of 11111… to left so that we have i zeros on the right hand side and then we can get the required mask.