473,405 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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.
Nov 15 '05 #1
4 14323
Also, or'ing instead of adding the bytes together might provide a
small performance benefit.
--
http://www.kynosarges.de
Nov 15 '05 #2
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" <br************@mchsi.com.nospam> wrote in message
news:uU**************@TK2MSFTNGP10.phx.gbl...
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).

Nov 15 '05 #3
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
-----Original Message-----
Egad - it's late and my brain hurts so I hope someone can help me out. Icurrently have a byte array. Lets say the first 2 bytes in this arrayrepresent a 16 bit number. I'm currently taking the first byte and bitshifting 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 myloops when the bytes get to be 8 bytes long for example (in which case I bitshift the first byte to to the left 8 places and I do this 7 times, the nextbyte I bit shift 6 times and so on). Can someone enlighten me to an easierway of doing this. Lets say I have

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

Thanks!

Brian

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

.

Nov 15 '05 #4
guy
or ...
use unsafe code, point at byte array, read back a
short / int or whatever.

-----Original Message-----
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 bitshift 8 times

hth

guy
-----Original Message-----
Egad - it's late and my brain hurts so I hope someone
canhelp me out. I
currently have a byte array. Lets say the first 2 bytesin this array
represent a 16 bit number. I'm currently taking the

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


.

.

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: w3r3w0lf | last post by:
hello! I have a following situation: I have a byte array where at a certain location are stored 4 bytes, and these should be "put" into long variable (or any other 4 byte one). ie: byte...
9
by: mprocopio | last post by:
Fellow JavaScripters, I am looking for code or implementation ideas for converting an integer variable to a four-byte array. I'm porting some of my code from C#, where I make use of their...
4
by: cjw | last post by:
I need to read an 'unsigned char' array and save it as a bit array (values are only 0s or 1s). When processing, have to do math operations such as summing up the values etc. Pl let me know an...
40
by: aku | last post by:
I'm looking for the absolute fastest way to count the nr of bits that are set to "1" in a string. Presumably I then first need the fastest way to do this in a byte. I think this is it, but...
3
by: Pablo Gutierrez | last post by:
I have a C# method that reads Binary data (BLOB type) from a database and returns the data an array of bytes (i.e byte outbyte = new byte;). The BLOB column is saved into the database by a C...
25
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
33
by: Benjamin M. Stocks | last post by:
Hello all, I've heard differing opinions on this and would like a definitive answer on this once and for all. If I have an array of 4 1-byte values where index 0 is the least signficant byte of a...
16
by: johannblake | last post by:
I have a variable that is 1 bit wide. I also have a variable that is a byte. I want to shift the bits out of the byte into the bit variable (one at a time) but am not sure how to do this or whether...
8
by: Polaris431 | last post by:
I have a buffer that holds characters. Four characters in a row represent an unsigned 32 bit value. I want to convert these characters to a 32 bit value. For example: char buffer; buffer =...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.