1. Code:
int blah( unsigned char b )
{
return b&1?0:b&2?1:b&4?2:b&8?3:b&16?4:b&32?5:b&64?6:b&128?7:-1;
}
Assuming you don't want a look up table, as mentioned before, a series of if checks is as fast as you're going to get. However, I don't see the point of doing any math (dividing in a loop, etc), when you can just AND each bit. This assumes you want eight channels, numbered from zero to seven, with -1 for error.

Quzah. 2. I would imagine an enumeration of your channel listing 8 or up to 32, then logical AND the input to those values to get what you need. 3. For a 32-bit target operand the lookup table approach is pretty expensive in terms of storage and pushes the OS into doin' paging / swapping. IMO, one of the bit scan instructions is the way to go. 4. Originally Posted by itCbitC
For a 32-bit target operand the lookup table approach is pretty expensive in terms of storage
That is why you would use a smaller lookup table, which is kind of a hybrid between checking each bit individually and having an entry in the lookup table for each possible input. 5. Originally Posted by chakra if byte has the value 3 (0000 0011), I need to say 7th channel is free of usage.
if the value is 255(1111 1111), then 1st channel is free for usage .

I need to arrive at this decision ASAP, as this function is expected to be heavily used.
Looping around the bits will not be a feasible solution.
Do a bitwise negation of the value and use my Computing Trailing Zeros HOWTO for a constant time check.

Greets,
Philip 6. Originally Posted by Snafuist Do a bitwise negation of the value and use my Computing Trailing Zeros HOWTO for a constant time check.

Greets,
Philip
That is certainly an interesting idea, but I'd kind of like to see how you think this will not be the slowest method? 7. Originally Posted by Snafuist Do a bitwise negation of the value and use my Computing Trailing Zeros HOWTO for a constant time check.

Greets,
Philip
How would that answer the question? 8. Code:
if byte
do what was done above, sanz returning -1
if byte
...
...
return -1
Should end up with something like a maximum of four masks and a possible eleven IF checks, for a 32bit number. You could drop seven of those if you used a lookup table, so four masks and a possible four table checks.

Quzah. 9. Originally Posted by tabstop How would that answer the question?
For one byte, 8 - the number of trailing zeros (after inversion) will be the first free channel. However, considering the process involved, it might be like assembling a bicycle in order to cross the street. 10. Originally Posted by laserlight That is why you would use a smaller lookup table, which is kind of a hybrid between checking each bit individually and having an entry in the lookup table for each possible input.
If I get it, what happens if the value lies outside the index of values in the lookup table? 11. Originally Posted by MK27 That is certainly an interesting idea, but I'd kind of like to see how you think this will not be the slowest method?
Computing the number of trailing zeros takes exactly 5 operations (independent of the size of the input number). Consider the following code (for computing the NTZ in 32 bits)

Code:
unsigned int v;  // find the number of trailing zeros in 32-bit v
int r;           // result goes here
static const int debruijn =
{
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
r = debruijn[((v & -v) * 0x077CB531UL) >> 27];
The 5 operations are:
- bitwise AND
- subtraction
- multiplication
- bitshift
- array lookup

Can you do it in less than 5 ops?

Greets,
Philip 12. Originally Posted by itCbitC If I get it, what happens if the value lies outside the index of values in the lookup table?
How will a one byte value do that? Or, for the 32-bit value, what int? 13. Originally Posted by Snafuist Can you do it in less than 5 ops?

Greets,
Philip
Yeah, I can do it in 1 with a lookup table. Quzah. 14. Originally Posted by MK27 For one byte, 8 - the number of trailing zeros (after inversion) will be the first free channel. However, considering the process involved, it might be like assembling a bicycle in order to cross the street.
Except, of course, it won't. Consider the input 10101010.... 15. Originally Posted by tabstop Except, of course, it won't. Consider the input 10101010....
Gosh tabstop, that is a pretty hard objection to object to. Popular pages Recent additions 