Solved

Number of bits set in an unsigned char

Posted on 2003-11-23
2
390 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
[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
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

631 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