News Score: Score the News, Sort the News, Rewrite the Headlines

Why does x & -x equal to the largest power of 2 that divides x?

25 May 2024I recently read in a book that x & -x equals to the largest power of 2 that divides x. For example, 13 & -13 is 1 (2^0), 24 & -24 is 8 (2^3), 14 & -14 is 2 (2^1). Here & means bitwise AND. May be this is just me, but after reading it, I was not able to understand the reasoning. Obviously, & is a bitwise operator and 2 is the base, so there is a connection. But what is the connection actually?Representation of Negative Numbers in BinaryWe should actually start with how numbers are repr...

Read more at arunmani.in

© News Score  score the news, sort the news, rewrite the headlines