Patrick Hoonhout <plh650@hotmail.com> scribbled the following:[color=blue]
> Hello,[/color]
[color=blue]
> Trying to get the bit offset value from a byte. For example:[/color]
[color=blue]
> 0x1 = 0
> 0x2 = 1
> 0x4 = 2
> 0x8 = 3
> 0x10 = 4
> ..
> ..
> 0x80 = 7
> etc.[/color]
[color=blue]
> I need this value so I can use the shift operator '<<' or '>>'. I can think
> of a number of ways to do this but they all seem to be long in code.[/color]
[color=blue]
> I can only think there is some quick bitwise operation to get the bit offset
> value.[/color]
[color=blue]
> (please provide code in 'C')[/color]
Is this for your homework? Here's one quick and dirty way:
int byte=getbyte();int bit=0;while(byte>>=1)bit++;
This code hasn't been tested, it is only guaranteed to work for those
byte values you posted, and it modifies the original byte value.
It is left as an exercise to make this work for other byte values,
provide error checking, proper input and ouput, and of course comment
and document it.
--
/-- Joona Palaste (palaste@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
|
http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Normal is what everyone else is, and you're not."
- Dr. Tolian Soran