tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

64-bit masks



Hi I'm writing a shared library and have a two part question:

1. What is the correct (portable?) way of specifying 64-bit constants?

If I need a 64-bit constant set to all 1s, will the following work on all systems

unit64_t num = 0xffffffffffffffffULL;


2. I have an array of 64 objects that I need to keep track of. I keep their state (i.e. busy/free) in a 64-bit mask, 1 for busy, 0 for free.

unit64_t state;

Each object has an id, from 0 to 63. The bit position in the 'state' mask corresponds to object's id. So if object with id=5 becomes busy, it sets bit 5 in 'state' variable to 1.

Multiple objects can be busy/free at the same time. I need a fast way to look at the 'sate' variable and tell which objects are busy, or free. Below is one algorithm I came up with, can anyone suggest something faster?


uint64_t current_state = state;

for(i=0; i<64; i++)
{
        current_state >>= i;

        if(current_state == (0xffffffffffffffffULL >> i))
                break; /* all objects are busy */

        if((current_state & 0x1U) == 0)
                printf("object %d is free\n", i);
        
}


Home | Main Index | Thread Index | Old Index