diff --git a/libraries/shared/src/SharedUtil.cpp b/libraries/shared/src/SharedUtil.cpp index e5aded2798..882d4719c8 100644 --- a/libraries/shared/src/SharedUtil.cpp +++ b/libraries/shared/src/SharedUtil.cpp @@ -130,14 +130,22 @@ void outputBits(unsigned char byte, QDebug* continuedDebug) { } int numberOfOnes(unsigned char byte) { - return (byte >> 7) - + ((byte >> 6) & 1) - + ((byte >> 5) & 1) - + ((byte >> 4) & 1) - + ((byte >> 3) & 1) - + ((byte >> 2) & 1) - + ((byte >> 1) & 1) - + (byte & 1); + + static const int nbits[256] = { + 0,1,1,2,1,2,2,3,1,2,2,3,2,3,3,4,1,2,2,3,2,3,3,4,2,3,3, + 4,3,4,4,5,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2,3,3,4,3,4, + 4,5,3,4,4,5,4,5,5,6,1,2,2,3,2,3,3,4,2,3,3,4,3,4,4,5,2, + 3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3,3,4,3,4,4,5,3,4,4,5, + 4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,1,2,2,3,2,3,3, + 4,2,3,3,4,3,4,4,5,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,2,3, + 3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6,4,5,5,6,5, + 6,6,7,2,3,3,4,3,4,4,5,3,4,4,5,4,5,5,6,3,4,4,5,4,5,5,6, + 4,5,5,6,5,6,6,7,3,4,4,5,4,5,5,6,4,5,5,6,5,6,6,7,4,5,5, + 6,5,6,6,7,5,6,6,7,6,7,7,8 + }; + + return nbits[(unsigned char) byte]; + } bool oneAtBit(unsigned char byte, int bitIndex) {