I really liked nv3's Solution. I gave it five stars. I liked it so much that I decided to try it out and develop a VB .NET program to count bits using nv3's method. I know you wanted an example in C or C++ but VB .NET is easier for me. You could use one of the free
source code translators[
^] to convert it to C# to get closer to C or C++ syntax.
First, I wrote a small program to generate the lookup table and copy the values to the clipboard so I could paste them into my bit count program.
Dim x As Integer
Dim y As Integer
Dim z As Integer
Dim s As String = "{"
For x = 0 To 255
z = 0
Dim b As New BitArray({x})
For y = 0 To 31
z += DirectCast(IIf(b(y), 1, 0), Integer)
Next
s += z.ToString & ","
Next
s = s.Substring(0, s.Length - 1) & "}"
Clipboard.SetData(DataFormats.Text, s)
Then, I developed the following program to do the computation. I used Unsigned Integer because bit shift operations on Signed Integers propagate the sign value. The
Dim arrValues() As Integer
is set to the string of values that I computed in my first program.
Dim arrValues() As Integer = {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}
Dim intValue As UInteger = 2433815039 ' How many bits=1 in this value?
Dim intTemp As UInteger
Dim intCountOfBits As Integer = 0
'
' An unsigned integer has four groups of 8 bits (Total of 32 bits)
' 8 Bits holds values ranging from 0 to 255.
' For the comments below, I have numbered the four groups (1-4) from left to right
'
intTemp = intValue >> 24 ' Shift right 24 bits to remove leftmost 8 bits (Group 1)
intCountOfBits = arrValues(intTemp) ' Lookup number of bits that are on in this group of 8 bits (index=0-255)
intTemp = intValue << 8 ' Shift left 8 bits to remove leftmost 8 bits
intTemp >>= 24 ' Shift right 24 bits to isolate second group of 8 bits
intCountOfBits += arrValues(intTemp) ' Lookup number of bits that are on in this group of 8 bits (index=0-255)
intTemp = intValue << 16 ' Shift left 16 bits to remove leftmost 16 bits
intTemp >>= 24 ' Shift right 24 bits to isolate third group of 8 bits
intCountOfBits += arrValues(intTemp) ' Lookup number of bits that are on in this group of 8 bits (index=0-255)
intTemp = intValue << 24 ' Shift left 24 bits to remove leftmost 24 bits
intTemp >>= 24 ' Shift right 24 bits to isolate fourth group of 8 bits
intCountOfBits += arrValues(intTemp) ' Lookup number of bits that are on in this group of 8 bits (index=0-255)
MsgBox(intCountOfBits.ToString)