Byte math and bit shifting

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Brian Patterson

    #1

    Byte math and bit shifting

    Egad - it's late and my brain hurts so I hope someone can help me out. I
    currently have a byte array. Lets say the first 2 bytes in this array
    represent a 16 bit number. I'm currently taking the first byte and bit
    shifting it to the left by 8 and then adding this value to the next byte.
    This gives me the number I need but I'm not liking the performance of my
    loops when the bytes get to be 8 bytes long for example (in which case I bit
    shift the first byte to to the left 8 places and I do this 7 times, the next
    byte I bit shift 6 times and so on). Can someone enlighten me to an easier
    way of doing this. Lets say I have

    0x01 and 0x02 in my byte array. this number should be 258 but how can I
    easily convert this in code?

    Thanks!

    Brian

    NOTE: Remove upper can from email address to email me directly.


  • Christoph Nahr

    #2
    Re: Byte math and bit shifting

    Also, or'ing instead of adding the bytes together might provide a
    small performance benefit.
    --
    Stay ahead in World of Warcraft with expert guides, latest patch news, class tips, dungeon strategies, PvP builds, and The War Within updates—all in one place.

    Comment

    • Simon Trew

      #3
      Re: Byte math and bit shifting

      Well why can't you shift the byte in one stage rather than seven? Could you
      give a short but complete example of the algorithm and data structure you're
      using?

      S.

      "Brian Patterson" <brianpatterson @mchsi.com.nosp am> wrote in message
      news:uUqCKgAVDH A.2164@TK2MSFTN GP10.phx.gbl...[color=blue]
      > Egad - it's late and my brain hurts so I hope someone can help me out. I
      > currently have a byte array. Lets say the first 2 bytes in this array
      > represent a 16 bit number. I'm currently taking the first byte and bit
      > shifting it to the left by 8 and then adding this value to the next byte.
      > This gives me the number I need but I'm not liking the performance of my
      > loops when the bytes get to be 8 bytes long for example (in which case I[/color]
      bit[color=blue]
      > shift the first byte to to the left 8 places and I do this 7 times, the[/color]
      next[color=blue]
      > byte I bit shift 6 times and so on).[/color]


      Comment

      • guy

        #4
        Byte math and bit shifting

        why not try putting the high order byte in an int
        andmultipling by 256? the performance hit of the
        multiply *** may *** well be lessthan looping round a bit
        shift 8 times

        hth

        guy[color=blue]
        >-----Original Message-----
        >Egad - it's late and my brain hurts so I hope someone can[/color]
        help me out. I[color=blue]
        >currently have a byte array. Lets say the first 2 bytes[/color]
        in this array[color=blue]
        >represent a 16 bit number. I'm currently taking the first[/color]
        byte and bit[color=blue]
        >shifting it to the left by 8 and then adding this value[/color]
        to the next byte.[color=blue]
        >This gives me the number I need but I'm not liking the[/color]
        performance of my[color=blue]
        >loops when the bytes get to be 8 bytes long for example[/color]
        (in which case I bit[color=blue]
        >shift the first byte to to the left 8 places and I do[/color]
        this 7 times, the next[color=blue]
        >byte I bit shift 6 times and so on). Can someone[/color]
        enlighten me to an easier[color=blue]
        >way of doing this. Lets say I have
        >
        >0x01 and 0x02 in my byte array. this number should be[/color]
        258 but how can I[color=blue]
        >easily convert this in code?
        >
        >Thanks!
        >
        >Brian
        >
        >NOTE: Remove upper can from email address to email me[/color]
        directly.[color=blue]
        >
        >
        >.
        >[/color]

        Comment

        • guy

          #5
          Byte math and bit shifting

          or ...
          use unsafe code, point at byte array, read back a
          short / int or whatever.

          [color=blue]
          >-----Original Message-----
          >why not try putting the high order byte in an int
          >andmultiplin g by 256? the performance hit of the
          >multiply *** may *** well be lessthan looping round a[/color]
          bit[color=blue]
          >shift 8 times
          >
          >hth
          >
          >guy[color=green]
          >>-----Original Message-----
          >>Egad - it's late and my brain hurts so I hope someone[/color][/color]
          can[color=blue]
          >help me out. I[color=green]
          >>currently have a byte array. Lets say the first 2 bytes[/color]
          >in this array[color=green]
          >>represent a 16 bit number. I'm currently taking the[/color][/color]
          first[color=blue]
          >byte and bit[color=green]
          >>shifting it to the left by 8 and then adding this value[/color]
          >to the next byte.[color=green]
          >>This gives me the number I need but I'm not liking the[/color]
          >performance of my[color=green]
          >>loops when the bytes get to be 8 bytes long for example[/color]
          >(in which case I bit[color=green]
          >>shift the first byte to to the left 8 places and I do[/color]
          >this 7 times, the next[color=green]
          >>byte I bit shift 6 times and so on). Can someone[/color]
          >enlighten me to an easier[color=green]
          >>way of doing this. Lets say I have
          >>
          >>0x01 and 0x02 in my byte array. this number should be[/color]
          >258 but how can I[color=green]
          >>easily convert this in code?
          >>
          >>Thanks!
          >>
          >>Brian
          >>
          >>NOTE: Remove upper can from email address to email me[/color]
          >directly.[color=green]
          >>
          >>
          >>.
          >>[/color]
          >.
          >[/color]

          Comment

          Working...