Solved

Number of bits set in an unsigned char

Posted on 2003-11-23
2
347 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
Comment Utility
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
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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…
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.

762 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

12 Experts available now in Live!

Get 1:1 Help Now