Byte math and bit shifting

  • Thread starter Thread starter Brian Patterson
  • Start date Start date
B

Brian Patterson

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.
 
Also, or'ing instead of adding the bytes together might provide a
small performance benefit.
 
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.
 
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
 
Back
Top