Descriptionmath/go: added BitCount() to Int.
To count the number of bits set in Int, the BitCount() method was added.
Corresponding bitCount() functions were added to nat.go and arith.go.
The underlying func bitCount(x Word) (n int) uses the Wegner algorithm.
It is not the best algorithm (particularly when there are many 1's),
but it is simple and passes written test cases.
The commented out block of code above corresponds to another
algorithm: variable-precision SWAR algorithm. However, this code did
not pass the test cases.
Perhaps it had something to do with integer overflow?
Regardless, the Wegner algorithm worked well and passes all test
cases added to int_test.go.
The bitCount function should perhaps return uint64 instead to
guard against overflow. But the return variable is set as int
in order to be consistent with the bitLen function and friends.
Patch Set 1 #Patch Set 2 : diff -r 778c06919be6 https://code.google.com/p/go/ #Patch Set 3 : diff -r a7984efbf183 https://code.google.com/p/go/ #
MessagesTotal messages: 6
|
||||||||||||||||||||||||||||||||||||||||||||||