?
Solved

A c program to test if a bit is set in an unsigned long array basically

Posted on 2005-04-25
8
Medium Priority
?
304 Views
Last Modified: 2010-04-15
A c program to test if a bit is set in an unsigned long array

I need something similiar to test_bit() in bitops.h

Basically I have any unsigned long array and I want to know if each bit in this array is set or not.

#define BITS_PER_LONG (sizeof(unsigned long) * 8)
#define BIT_MAP_SIZE  ( (256 + BITS_PER_LONG - 1) / BITS_PER_LONG)
unsigned long array[BITMAP_SIZE]

How do I find if each bit is set or not.

0
Comment
Question by:sanjay_thakur
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13863258
Look at this very recent question, look similar to your needs:
http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21400697.html
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 13864015
#define ISSET(VAR,X) ((VAR) & (1<<(X)))

This simple one line macro does the trick ... VAR is the unsigned long or any variable which you need to check and X is the bit number starting from least significant bit (LSB) and couting from 0.
e.g.

if ( ISSET(mylong, 3))
      printf ("bit number 3 is set\n");
note that we are counting from 0 ... If you wish to count from 1, modify macro to have X-1 instead of X
0
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13864682
if the above macro is too complicated for u try this


assuming that long u have is of 4 bytes

void test_bit(long int array[], int sizeof_array)
{

for (int i=0; i<sizeof_array; i++)
{
if (array[i] && 1)  printf(" Bit 1 is set ");
if (array[i] && 2) printf(" Bit 2 is set ");
if (array[i] && 4) printf(" Bit 3 is set ");
if (array[i] && 8) printf(" Bit 4 is set ");
if (array[i] && 16) printf(" Bit 5 is set ");
if (array[i] && 32) printf(" Bit 6 is set ");
if (array[i] && 64) printf(" Bit 7 is set ");
if (array[i] && 128) printf(" Bit 8 is set ");
if (array[i] && 256) printf(" Bit 9 is set ");
if (array[i] && 512) printf(" Bit 10 is set ");
if (array[i] && 1024) printf(" Bit 11 is set ");
if (array[i] && 2048) printf(" Bit 12 is set ");
if (array[i] && 4096 ) printf(" Bit 13 is set ");
if (array[i] && 8192) printf(" Bit 14 is set ");
if (array[i] && 16384) printf(" Bit 15 is set ");
if (array[i] && 32768 ) printf(" Bit 16 is set ");
}//for


}// end of function


not very efficent but should work
0
Independent Software Vendors: 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!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 13864704
Not very efficient, takes up lot more space, and does not work ;-) ... Each of the && should have been & instead

It is better done in a loop that unrolling it out

for ( i=0; i < sizeof(long)*8; i++ )
{
       if ( 1 == var & (1<<i) )
                printf ("bit %d is set\n",i);
}
0
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13865579
thanks sunnycoder for the correction


mr sanjay thakur plz note the correction made by sunnycoder

the line shoud be
if (array[i] & 16384) printf(" Bit 15 is set ");  not   if (array[i] && 16384) printf(" Bit 15 is set ");

ie if u bother to use my code snippet which is not very efficent but does expalin how it should be done
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13868618
int get_bit(unsigned long arr[], int bitnum) {
    int idx = bitnum / BITS_PER_LONG;
    int mask = (((unsigned long)1) << (bitnum % BITS_PER_LONG));
    return (arr[idx] & mask) != 0;
}

void set_bit(unsigned long arr[], int bitnum, int value) {
    int idx = bitnum / BITS_PER_LONG;
    int mask = (((unsigned long)1) << (bitnum % BITS_PER_LONG));
    if (value) {
        arr[idx] |= mask;
    } else {
        arr[idx] &= ~mask;
    }
}  
0
 
LVL 22

Expert Comment

by:NovaDenizen
ID: 13868621
dangit, 'int mask' above should be 'unsigned long mask' in both routines.
0
 
LVL 16

Accepted Solution

by:
PaulCaswell earned 150 total points
ID: 13870958
>>dangit, 'int mask' above should be 'unsigned long mask' in both routines.
Or, to avoid all local variables, improve portability and remove ALL assumptions on size:

#define bit(arr,bitnum) ((arr[bitnum / BITS_PER_LONG] & (((unsigned long)1) << (bitnum % BITS_PER_LONG))))

Really, the code DOES document itself.

Paul
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.
Suggested Courses
Course of the Month16 days, 8 hours left to enroll

862 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