Solved

Number of bits set in an unsigned char

Posted on 2003-11-23
2
379 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…

756 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