Solved

Number of bits set in an unsigned char

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

813 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

10 Experts available now in Live!

Get 1:1 Help Now