bitwise operation...

Discussion in 'C Programming' started by Patrick Hoonhout, Aug 27, 2003.

  1. 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.
     
    Patrick Hoonhout, Aug 27, 2003
    #1
    1. Advertising

  2. Patrick Hoonhout <> 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 () ---------------------------\
    | 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
     
    Joona I Palaste, Aug 27, 2003
    #2
    1. Advertising

  3. "Alan Balmer" <> 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
    >


    Yes, just one bit set.

    Patrick.
     
    Patrick Hoonhout, Aug 27, 2003
    #3
  4. Patrick Hoonhout

    Jirka Klaue Guest

    Patrick Hoonhout wrote:
    > "Alan Balmer" <> 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
     
    Jirka Klaue, Aug 27, 2003
    #4
  5. 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)
     
    Jarno A Wuolijoki, Aug 27, 2003
    #5
  6. In article <-berlin.de>,
    Jirka Klaue <-berlin.de> wrote:
    >Patrick Hoonhout wrote:
    >> "Alan Balmer" <> 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
     
    Brett Frankenberger, Aug 27, 2003
    #6
  7. Patrick Hoonhout

    Jirka Klaue Guest

    Brett Frankenberger wrote:
    > Jirka Klaue <-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
     
    Jirka Klaue, Aug 28, 2003
    #7
  8. "Jirka Klaue" <-berlin.de> wrote in message
    news:bijghd$9qc$-Berlin.DE...
    > Patrick Hoonhout wrote:
    > >"Jarno A Wuolijoki" <> 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.
     
    Patrick Hoonhout, Aug 28, 2003
    #8
  9. Patrick Hoonhout

    Severian Guest

    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)&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;
    }
     
    Severian, Aug 28, 2003
    #9
  10. 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.
     
    Jarno A Wuolijoki, Aug 28, 2003
    #10
  11. On Thu, 28 Aug 2003, Severian wrote:

    > 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
    :)
     
    Jarno A Wuolijoki, Aug 28, 2003
    #11
  12. Patrick Hoonhout

    Alan Balmer Guest

    On Wed, 27 Aug 2003 16:10:50 -0700, "Patrick Hoonhout"
    <> wrote:

    >
    >"Jarno A Wuolijoki" <> wrote in message
    >news:p...
    >> 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)
    >>

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

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Aug 28, 2003
    #12
  13. Patrick Hoonhout

    Soji Guest

    Jirka Klaue <-berlin.de> wrote in message news:<bijfrn$991$-Berlin.DE>...
    > Brett Frankenberger wrote:
    > > Jirka Klaue <-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


    Try:

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

    Soji
     
    Soji, Aug 28, 2003
    #13
  14. Patrick Hoonhout

    Alan Balmer Guest

    On Thu, 28 Aug 2003 19:22:28 +0000 (UTC), (Brett
    Frankenberger) wrote:

    >In article <>,
    >Soji <> wrote:
    >>
    >>Offset = (int) (log(num_byte)/log(2.00))

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

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Aug 28, 2003
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pasquale Imbemba

    Bitwise Operation

    Pasquale Imbemba, May 6, 2004, in forum: Java
    Replies:
    2
    Views:
    553
    Roedy Green
    May 7, 2004
  2. biswaranjan.rath

    bitwise AND operation in xslt

    biswaranjan.rath, May 8, 2006, in forum: XML
    Replies:
    3
    Views:
    4,915
    shaunroe
    Nov 12, 2008
  3. Magix

    Bitwise operation

    Magix, Jul 30, 2004, in forum: C Programming
    Replies:
    6
    Views:
    743
    Joe Wright
    Jul 31, 2004
  4. Magix

    Bitwise Operation

    Magix, Oct 15, 2004, in forum: C Programming
    Replies:
    5
    Views:
    561
    Dimension
    Oct 15, 2004
  5. Jerry

    Bitwise operation for division

    Jerry, Mar 2, 2005, in forum: C Programming
    Replies:
    7
    Views:
    8,261
    Chris Williams
    Mar 2, 2005
Loading...

Share This Page