Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Number of bits set in an unsigned char

Posted on 2003-11-23
Medium Priority
402 Views
Is there a rapid way of finding the number of bits set by an unsigned char?

For example,

unsigned char c1=63; //0x3f, binary: 0011 1111
unsigned char c2=67; //0x43, binary: 0100 0011

For c1, the answer I want is 6, and for c2 the answer I want is 3.

I'm going to be doing this calculation A LOT as part of an interative analysis program, and wondered if I should just set up some array like the following. Really would love a super rapid way of evaluating this number.

....
myarray(63) = 6;
....
myarray(67) = 3;
....
myarray(255) = 8;

0
Question by:greengills
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 86

Accepted Solution

jkr earned 300 total points
ID: 9807903
Use

unsigned char countbits ( unsigned char b) {

if ( b & mask) {

count++;
}
}

return ( count);
}
0

Expert Comment

ID: 9824604
Hi,

Instead of calculating number of bits set each time, Create a class and get the result quickly by passing the unsigned char as argument.

Hope the following code might be helpful.

Thanks and Regards,
Ramesh Ramasamy.

Cut and paste the appropriate portion of the code.
//---------------------------------------------------------------------------
#include <stdio.h>
#pragma hdrstop

//---------------------------------------------------------------------------

class MYARRAY {
public:
MYARRAY ();
int operator[](unsigned char n) { return nBits[n]; }
private:
int nBits[256];
};

MYARRAY::MYARRAY() {
int cBits;
for (i = 0; i <= 255; i++) {
if ( i & mask) {
cBits++;
}
}
nBits[i] = cBits;
if (i == 255) break;
}
}

#pragma argsused
int main(int argc, char* argv[])
{
// Create an object
MYARRAY MyArray;

// Get the number of bits set by wrting the following statement.
// MyArray[unsigned char];

// To verify whether no. of bits are set correctly
FILE * fp = fopen ("NoOfBits.txt", "wt");
for (unsigned char i = 0; i <= 255; i++) {
fprintf (fp, "char: %d = %d\n", i, MyArray[i]);
if (i == 255) break;
}

return 0;
}
//---------------------------------------------------------------------------
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
###### Suggested Courses
Course of the Month6 days, 23 hours left to enroll