bitwise operation...

P

Patrick Hoonhout

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

Joona I Palaste

Patrick Hoonhout said:
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 ([email protected]) ---------------------------\
| 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
 
P

Patrick Hoonhout

Alan Balmer said:
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.

Patrick.
 
J

Jirka Klaue

Patrick said:
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
 
J

Jarno A Wuolijoki

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)
 
B

Brett Frankenberger

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
 
J

Jirka Klaue

Brett said:
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
 
P

Patrick Hoonhout

Jirka Klaue said:
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.
 
S

Severian

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)&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, bitof(b));
return 0;
}
 
J

Jarno A Wuolijoki

Ugly... but -- no branches! I imagine it could be improved
considerably.
((036452710>>((((b-1)^((b-1)>>4)^(((b-1)&0x08)>>2))&0x7)*3))&0x07)

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

Alan Balmer

Sweet! Thanks...

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

Alan Balmer

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
Yah, but it's "cool."
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

shift a block of memory content 7
bit position 12
Scoped enum question 21
bitwise operator and endianness 5
bitwise decimal operators 2
Incrementing Values 1
VERY newbie pointer issue 6
Bitwise Operation limitation? 8

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top