0

What does j-=j & (-j) mean

6th Feb 2021, 9:33 AM
Coder
Coder - avatar
4 Answers
+ 7
You have 2 parts to this expression 1) a-=b is same as ( a = a - b ) 2) ( a & b ) gives bitwise AND of the *a* and *b* ( logical AND of each bit in the binary representation of the numbers ) Now in case of j-=j&(-j) It becomes j = j - (j&(-j)) And now as both * j * and * - j * will only differ at sign bit so the bitwise AND of both would simply be absolute value of *j* So for positive value of * j * the result will always be zero and for negative values of * j * it will be double the value in * j *
6th Feb 2021, 10:30 AM
Arsenic
Arsenic - avatar
+ 2
Coder you got that right.
6th Feb 2021, 1:13 PM
Arsenic
Arsenic - avatar
+ 1
Thanks for explaining dude
6th Feb 2021, 1:18 PM
Coder
Coder - avatar
0
So if j=1 Then In binary. sign bit +j. = (0) 1 -j = (1) 1 ------------- j&(-j) = (0) 1 and j-=1 so j=1-1=0 ???????
6th Feb 2021, 1:09 PM
Coder
Coder - avatar