Connecting Tech Pros Worldwide Forums | Help | Site Map

bitwise operation...

Patrick Hoonhout
Guest
 
Posts: n/a
#1: Nov 13 '05
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')

TIA

Patrick.



Joona I Palaste
Guest
 
Posts: n/a
#2: Nov 13 '05

re: bitwise operation...


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
Patrick Hoonhout
Guest
 
Posts: n/a
#3: Nov 13 '05

re: bitwise operation...



"Alan Balmer" <albalmer@att.net> wrote in message > >[color=blue]
> 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
> removebalmerconsultingthis@att.net[/color]

Yes, just one bit set.

Patrick.


Jirka Klaue
Guest
 
Posts: n/a
#4: Nov 13 '05

re: bitwise operation...


Patrick Hoonhout wrote:[color=blue]
> "Alan Balmer" <albalmer@att.net> wrote in message > >
>[color=green]
>>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?[/color]
>
> Yes, just one bit set.[/color]

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
#5: Nov 13 '05

re: bitwise operation...


On Wed, 27 Aug 2003, Patrick Hoonhout wrote:
[color=blue]
> 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.[/color]

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

Brett Frankenberger
Guest
 
Posts: n/a
#6: Nov 13 '05

re: bitwise operation...


In article <3F4D112A.90201@ee.tu-berlin.de>,
Jirka Klaue <jklaue@ee.tu-berlin.de> wrote:[color=blue]
>Patrick Hoonhout wrote:[color=green]
>> "Alan Balmer" <albalmer@att.net> wrote in message > >
>>[color=darkred]
>>>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?[/color]
>>
>> Yes, just one bit set.[/color]
>
> 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;
> }[/color]

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

-- Brett

Jirka Klaue
Guest
 
Posts: n/a
#7: Nov 13 '05

re: bitwise operation...


Brett Frankenberger wrote:[color=blue]
> Jirka Klaue <jklaue@ee.tu-berlin.de> wrote:[/color]
....[color=blue][color=green]
>> 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;
>> }[/color]
>
> Which, given his input constraints (exactly one bit set), is equivalent
> to:
> off = 7;[/color]

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
#8: Nov 13 '05

re: bitwise operation...



"Jirka Klaue" <jklaue@ee.tu-berlin.de> wrote in message
news:bijghd$9qc$1@mamenchi.zrz.TU-Berlin.DE...[color=blue]
> Patrick Hoonhout wrote:[color=green]
> >"Jarno A Wuolijoki" <jwuolijo@cs.Helsinki.FI> wrote:[/color]
> ...[color=green][color=darkred]
> >>(a&0x55?1:0) + (a&0xcc?2:0) + (a&0xf0?4:0)[/color]
> >
> > Sweet! Thanks...[/color]
>
> 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
>[/color]

Yeah, simple fix...

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

Patrick.


Severian
Guest
 
Posts: n/a
#9: Nov 13 '05

re: bitwise operation...


On Wed, 27 Aug 2003 13:50:54 -0700, "Patrick Hoonhout"[color=blue]
>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.[/color]

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

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

- Sev

--
#include <stdio.h>

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

/* Alternately:

unsigned int bitof(unsigned int b)
{
b--;
return (036452710 >> (((b ^ (b>>4) ^ ((b & 0x08)>>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
#10: Nov 13 '05

re: bitwise operation...


On Thu, 28 Aug 2003, Jirka Klaue wrote:
[color=blue][color=green][color=darkred]
> >>(a&0x55?1:0) + (a&0xcc?2:0) + (a&0xf0?4:0)[/color]
> > Sweet! Thanks...[/color]
> It would be sweeter, if it would produce the right result.[/color]

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

Jarno A Wuolijoki
Guest
 
Posts: n/a
#11: Nov 13 '05

re: bitwise operation...


On Thu, 28 Aug 2003, Severian wrote:
[color=blue]
> Ugly... but -- no branches! I imagine it could be improved
> considerably.
> ((036452710>>((((b-1)^((b-1)>>4)^(((b-1)&0x08)>>2))&0x7)*3))&0x07)[/color]

376097>>2*((b+9)%11)&7
:)

Alan Balmer
Guest
 
Posts: n/a
#12: Nov 13 '05

re: bitwise operation...


On Wed, 27 Aug 2003 16:10:50 -0700, "Patrick Hoonhout"
<plh650@hotmail.com> wrote:
[color=blue]
>
>"Jarno A Wuolijoki" <jwuolijo@cs.Helsinki.FI> wrote in message
>news:Pine.LNX.4.44.0308280005030.5586-100000@melkinpaasi.cs.Helsinki.FI...[color=green]
>> On Wed, 27 Aug 2003, Patrick Hoonhout wrote:
>>[color=darkred]
>> > Thanks, I've thought of all the switch and loop alternatives. I was[/color][/color]
>hoping[color=green][color=darkred]
>> > there might have been a nifty multi-combination bitwise/shiftwise[/color][/color]
>solution.[color=green]
>>
>> (a&0x55?1:0) + (a&0xcc?2:0) + (a&0xf0?4:0)
>>[/color]
>
>Sweet! Thanks...
>
>Patrick.
>[/color]
Tastes vary, I suppose, but I would prefer any of the other proposals,
unless it were part of an obfuscated C contest.

Originally, you implied that "quick" was desirable. I'd be interested
to see some measurements comparing this with e.g., simple lookup - on
a platform of your choice.

--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net
Soji
Guest
 
Posts: n/a
#13: Nov 13 '05

re: bitwise operation...


Jirka Klaue <jklaue@ee.tu-berlin.de> wrote in message news:<bijfrn$991$1@mamenchi.zrz.TU-Berlin.DE>...[color=blue]
> Brett Frankenberger wrote:[color=green]
> > Jirka Klaue <jklaue@ee.tu-berlin.de> wrote:[/color]
> ...[color=green][color=darkred]
> >> 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;
> >> }[/color]
> >
> > Which, given his input constraints (exactly one bit set), is equivalent
> > to:
> > off = 7;[/color]
>
> Can't you see the break? It's printed in big white letters at
> the end of each case.
>
> Jirka[/color]

Try:

Offset = (int) (log(num_byte)/log(2.00))

Soji
Alan Balmer
Guest
 
Posts: n/a
#14: Nov 13 '05

re: bitwise operation...


On Thu, 28 Aug 2003 19:22:28 +0000 (UTC), rbf@panix.com (Brett
Frankenberger) wrote:
[color=blue]
>In article <827cbcbf.0308281050.15f7451c@posting.google.com >,
>Soji <soji@oduleye.freeserve.co.uk> wrote:[color=green]
>>
>>Offset = (int) (log(num_byte)/log(2.00))[/color]
>
>In additiona to being tremendously inefficient (on most
>implementationss) as compared to the other alternatives people have
>posted, it's not guaranteed to give the right answer.
>
> -- Brett[/color]
Yah, but it's "cool."

--
Al Balmer
Balmer Consulting
removebalmerconsultingthis@att.net
Closed Thread