Solved

Number of bits set in an unsigned char

Posted on 2003-11-23
2
357 Views
Last Modified: 2010-04-01
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;

Thanks in advance for your help.
0
Comment
Question by:greengills
2 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 75 total points
ID: 9807903
Use

unsigned char countbits ( unsigned char b) {

unsigned char mask, count;

      for ( count = 0, mask = 0x80; mask != 0; mask = ( mask >> 1)) {

            if ( b & mask) {

                  count++;
            }
      }

      return ( count);
}
0
 

Expert Comment

by:er_ramesh
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() {
    unsigned char mask, i;
    int cBits;
    for (i = 0; i <= 255; i++) {
        for (cBits = 0, mask = 0x80; mask != 0; mask = (mask >> 1)) {
            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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now