bitwise operation...

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Patrick Hoonhout

    bitwise operation...

    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

    #2
    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(byt e>>=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.hel sinki.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

    Comment

    • Patrick Hoonhout

      #3
      Re: bitwise operation...


      "Alan Balmer" <albalmer@att.n et> 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
      > removebalmercon sultingthis@att .net[/color]

      Yes, just one bit set.

      Patrick.


      Comment

      • Jirka Klaue

        #4
        Re: bitwise operation...

        Patrick Hoonhout wrote:[color=blue]
        > "Alan Balmer" <albalmer@att.n et> 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

        Comment

        • Jarno A Wuolijoki

          #5
          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)

          Comment

          • Brett Frankenberger

            #6
            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.n et> 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

            Comment

            • Jirka Klaue

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

              Comment

              • Patrick Hoonhout

                #8
                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.He lsinki.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.


                Comment

                • Severian

                  #9
                  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))&0 x7)*3))&0x07)

                  - Sev

                  --
                  #include <stdio.h>

                  #define bitof(b)
                  ((036452710>>(( ((b-1)^((b-1)>>4)^(((b-1)&0x08)>>2))&0 x7)*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;
                  }

                  Comment

                  • Jarno A Wuolijoki

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

                    Comment

                    • Jarno A Wuolijoki

                      #11
                      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))&0 x7)*3))&0x07)[/color]

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

                      Comment

                      • Alan Balmer

                        #12
                        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.He lsinki.FI> wrote in message
                        >news:Pine.LNX. 4.44.0308280005 030.5586-100000@melkinpa asi.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
                        removebalmercon sultingthis@att .net

                        Comment

                        • Soji

                          #13
                          Re: bitwise operation...

                          Jirka Klaue <jklaue@ee.tu-berlin.de> wrote in message news:<bijfrn$99 1$1@mamenchi.zr z.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

                          Comment

                          • Alan Balmer

                            #14
                            Re: bitwise operation...

                            On Thu, 28 Aug 2003 19:22:28 +0000 (UTC), rbf@panix.com (Brett
                            Frankenberger) wrote:
                            [color=blue]
                            >In article <827cbcbf.03082 81050.15f7451c@ posting.google. com>,
                            >Soji <soji@oduleye.f reeserve.co.uk> wrote:[color=green]
                            >>
                            >>Offset = (int) (log(num_byte)/log(2.00))[/color]
                            >
                            >In additiona to being tremendously inefficient (on most
                            >implementation ss) 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
                            removebalmercon sultingthis@att .net

                            Comment

                            Working...