Byte math and bit shifting

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

Christoph Nahr

Also, or'ing instead of adding the bytes together might provide a
small performance benefit.
 
S

Simon Trew

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

guy

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
 

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

Top