Sheldon <sh******@gmail.comwrote:

I forgot to include that BYTE is defined as unsigned char.

The return statement: return (one &(*value >bitnumber)); is cryptic.

Can you explain what it means?

Boolean evaluateBit(UWORD2 *value, int bitnumber) {

int one = 1;

return (one &(*value >bitnumber));

}

'Boolean' is also not defined;-) But what the function does is rather

simple: '*value' is supposed to be an unsigned interger. This value

is now right-shifted(*) 'bitnumber' times and the result is logically

and'ed with 1, i.e. resulting in 1 if the lowest bit after the right-

shift is set and 0 otherwise. If you call the function with 'bitnumber'

set to 0 it will return if the lowest bit of the value is set. If you

call it with 'bitnumber' set to 1 it tells you if the second-lowest bit

of the value is set. So, if you number the bits in ascending order, i.e.

0 is the lowest, 1 is the second lowest etc., the function tells you

if the bit of the value the function receives a pointer to and indexed

this way by 'bitnumber' is set.

Of course, there's no real good reason why the variable 'one' is

needed at all (and it actually should also be an unsigned int), you

could write the function as e.g.

unsigned int evaluateBit(unsigned int *value, int bitnumber) {

return ( *value >bitnumber ) & 1U;

}

Another way to write it would be

unsigned int evaluateBit(unsigned int *value, int bitnumber) {

return *value & ( 1U << bitnumber ) ? 1 : 0;

}

and there are probably countless other ways to do it.

Regards, Jens

(*) If you never heard about the "shifting thing": imagine the number

in binary notation. If you have 9 in decimal it's 1001 in binary.

When you now right-shift this by one position, you get 0100 (the

lowest bit gets thrown away), i.e. te result is 4. If you would

right-shift it by 2 positions you get 0010, i.e. 2 in decimal. You

can also left-shift, so if you again have 9, i.e. 1001 in binary,

then left-shifting it by one position gives you 10010, i.e. 18

in decimal - the lowest bit gets filled with a 0 bit.

--

\ Jens Thoms Toerring ___

jt@toerring.de
\__________________________

http://toerring.de