Velocity Reviews > bitwise operation...

# bitwise operation...

Patrick Hoonhout
Guest
Posts: n/a

 08-27-2003
Hello,

Trying to get the bit offset value from a byte. For example:

0x1 = 0
0x2 = 1
0x4 = 2
0x8 = 3
0x10 = 4
...
...
0x80 = 7
etc.

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.

I can only think there is some quick bitwise operation to get the bit offset
value.

TIA

Patrick.

Joona I Palaste
Guest
Posts: n/a

 08-27-2003
Patrick Hoonhout <(E-Mail Removed)> scribbled the following:
> Hello,

> Trying to get the bit offset value from a byte. For example:

> 0x1 = 0
> 0x2 = 1
> 0x4 = 2
> 0x8 = 3
> 0x10 = 4
> ..
> ..
> 0x80 = 7
> etc.

> 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.

> I can only think there is some quick bitwise operation to get the bit offset
> value.

> (please provide code in 'C')

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 ((E-Mail Removed)) ---------------------------\
| 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

Patrick Hoonhout
Guest
Posts: n/a

 08-27-2003

"Alan Balmer" <(E-Mail Removed)> wrote in message > >
> The quick way to map a byte into most any characteristic is to use a
> lookup table (256 entries, assuming CHAR_BIT is 8.) Are you always
> going to have only one bit set?
>
> --
> Al Balmer
> Balmer Consulting
> http://www.velocityreviews.com/forums/(E-Mail Removed)

Yes, just one bit set.

Patrick.

Jirka Klaue
Guest
Posts: n/a

 08-27-2003
Patrick Hoonhout wrote:
> "Alan Balmer" <(E-Mail Removed)> wrote in message > >
>
>>The quick way to map a byte into most any characteristic is to use a
>>lookup table (256 entries, assuming CHAR_BIT is 8.) Are you always
>>going to have only one bit set?

>
> Yes, just one bit set.

unsigned char byte = 0x80, off;

switch (byte) {
case 1: off = 0;
case 2: off = 1;
case 4: off = 2;
case 8: off = 3;
case 16: off = 4;
case 32: off = 5;
case 64: off = 6;
case 128: off = 7;
}

/* or */

int i;
unsigned char byte = 0x80, off, offs[256] = {0};
for (i=0; i<8; i++) offs[1 << i] = i;

off = offs[byte];

Jirka

Jarno A Wuolijoki
Guest
Posts: n/a

 08-27-2003
On Wed, 27 Aug 2003, Patrick Hoonhout wrote:

> Thanks, I've thought of all the switch and loop alternatives. I was hoping
> there might have been a nifty multi-combination bitwise/shiftwise solution.

(a&0x55?1:0) + (a&0xcc?2:0) + (a&0xf0?4:0)

Brett Frankenberger
Guest
Posts: n/a

 08-27-2003
In article <(E-Mail Removed)-berlin.de>,
Jirka Klaue <(E-Mail Removed)-berlin.de> wrote:
>Patrick Hoonhout wrote:
>> "Alan Balmer" <(E-Mail Removed)> wrote in message > >
>>
>>>The quick way to map a byte into most any characteristic is to use a
>>>lookup table (256 entries, assuming CHAR_BIT is 8.) Are you always
>>>going to have only one bit set?

>>
>> Yes, just one bit set.

>
> unsigned char byte = 0x80, off;
>
> switch (byte) {
> case 1: off = 0;
> case 2: off = 1;
> case 4: off = 2;
> case 8: off = 3;
> case 16: off = 4;
> case 32: off = 5;
> case 64: off = 6;
> case 128: off = 7;
> }

Which, given his input constraints (exactly one bit set), is equivalent
to:
off = 7;

-- Brett

Jirka Klaue
Guest
Posts: n/a

 08-27-2003
Brett Frankenberger wrote:
> Jirka Klaue <(E-Mail Removed)-berlin.de> wrote:

....
>> switch (byte) {
>> case 1: off = 0;
>> case 2: off = 1;
>> case 4: off = 2;
>> case 8: off = 3;
>> case 16: off = 4;
>> case 32: off = 5;
>> case 64: off = 6;
>> case 128: off = 7;
>> }

>
> Which, given his input constraints (exactly one bit set), is equivalent
> to:
> off = 7;

Can't you see the break? It's printed in big white letters at
the end of each case.

Jirka

Patrick Hoonhout
Guest
Posts: n/a

 08-28-2003

"Jirka Klaue" <(E-Mail Removed)-berlin.de> wrote in message
news:bijghd\$9qc\$(E-Mail Removed)-Berlin.DE...
> Patrick Hoonhout wrote:
> >"Jarno A Wuolijoki" <(E-Mail Removed)> wrote:

> ...
> >>(a&0x55?1:0) + (a&0xcc?2:0) + (a&0xf0?4:0)

> >
> > Sweet! Thanks...

>
> It would be sweeter, if it would produce the right result.
>
> 1 1
> 2 0
> 4 3
> 8 2
> 16 5
> 32 4
> 64 7
> 128 6
>
> Jirka
>

Yeah, simple fix...

(a&0xaa?1:0) + (a&0xcc?2:0) + (a&0xf0?4:0)

Patrick.

Severian
Guest
Posts: n/a

 08-28-2003
On Wed, 27 Aug 2003 13:50:54 -0700, "Patrick Hoonhout"
>Thanks, I've thought of all the switch and loop alternatives. I was hoping
>there might have been a nifty multi-combination bitwise/shiftwise solution.

Ugly... but -- no branches! I imagine it could be improved
considerably.

((036452710>>((((b-1)^((b-1)>>4)^(((b-1)&0x0>>2))&0x7)*3))&0x07)

- Sev

--
#include <stdio.h>

#define bitof(b)
((036452710>>((((b-1)^((b-1)>>4)^(((b-1)&0x0>>2))&0x7)*3))&0x07)

/* Alternately:

unsigned int bitof(unsigned int b)
{
b--;
return (036452710 >> (((b ^ (b>>4) ^ ((b & 0x0>>2)) & 0x7) * 3))
& 0x07;
}
*/

int main(void)
{
int i;
unsigned char b[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};

for (i=0; i<8; i++)
printf("%02x %2d\n", b[i], bitof(b[i]));
return 0;
}

Jarno A Wuolijoki
Guest
Posts: n/a

 08-28-2003
On Thu, 28 Aug 2003, Jirka Klaue wrote:

> >>(a&0x55?1:0) + (a&0xcc?2:0) + (a&0xf0?4:0)

> > Sweet! Thanks...

> It would be sweeter, if it would produce the right result.

Gotcha. I guess I'll go back testing before posting.