contains category wise problems(data structures, competitive) of popular platforms.
View the Project on GitHub mayankdutta/category-wise-problems
In layman terms hammering distance is the no. of times when both the no.s had different bits.
1: 0 0 0 1
4: 0 1 0 0
there were 2 places where both bits were different.
For example -
let n = 13 (1101).
1.
n-1 = 1100 => Each bit from rightmost set bit is flipped
n = n&(n-1) = 1100 => rightmost bit got unset
2. Now, n = 1100
n-1 = 1011 => Each bit from rightmost set bit is flipped
n = n&(n-1) = 1000 => rightmost bit unset
3. Now, n = 1000
n-1 = 0111 => Each bit from rightmost set bit is flipped
n = n&(n-1) = 0000 => rightmost bit unset
n becomes 0 and we get the count of set bits which is 3.
n - (n & -n)
.n ^ (n & (~n + 1))
n & (n-1)