Link to home
Start Free TrialLog in
Avatar of sanjay_thakur
sanjay_thakur

asked on

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

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.

Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Look at this very recent question, look similar to your needs:
https://www.experts-exchange.com/questions/21400697/Pointers-and-char-deciphering-code.html
Avatar of sunnycoder
#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
Avatar of furqanchandio
furqanchandio

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
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);
}
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
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;
    }
}  
dangit, 'int mask' above should be 'unsigned long mask' in both routines.
ASKER CERTIFIED SOLUTION
Avatar of PaulCaswell
PaulCaswell
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial