Calculating the negabinary representation of a given number without loops
问题 Could you provide a convincing explanation, or a mathematical proof, to why the following function calculates the negabinary representation of a given number? function quickNegabinary(number) { var mask = 0xAAAAAAAA; return ((number + mask) ^ mask).toString(2); } 回答1: Negabinary notation Negabinary notation uses a radix of -2. This means that, as in every number system with a negative base, every other bit has a negative value: position: ... 7 6 5 4 3 2 1 0 binary: ... 128 64 32 16 8 4 2 1