448,731 Members | 1,720 Online
Need help? Post your question and get tips & solutions from a community of 448,731 IT Pros & Developers. It's quick & easy.

# bit shifts by multiple bytes

 P: n/a I need to shift all of the values in a byte array by more than 8 bits, meaning that values should flow from one byte to another. Since I don't know in advance how many bits will be shifting, I can't do something easy like putting the bytes into a long or uint and shifting that. Let me give an example: If I want to shift this: 10010110 00001101 one byte to the right, I need to get: 01001011 00000110 10000000 Now I know how to figure out how many bytes to add onto the end of the array, but how do I shift the bits across bytes, as opposed to just lopping them off? Lee Mar 14 '06 #1
4 Replies

 P: n/a Lee, I would use a BitArray instance to handle this. What you can do is flip the bits accordingly in the bit array. Then, you can start at the end of the array, and copy over the bits appropriately, cycling through each position in the array. Then, you should be able to copy the bits back to a byte array. Hope this helps. -- - Nicholas Paldino [.NET/C# MVP] - mv*@spam.guard.caspershouse.com "Lee Crabtree" wrote in message news:eX**************@TK2MSFTNGP11.phx.gbl...I need to shift all of the values in a byte array by more than 8 bits,meaning that values should flow from one byte to another. Since I don'tknow in advance how many bits will be shifting, I can't do something easylike putting the bytes into a long or uint and shifting that. Let me givean example: If I want to shift this: 10010110 00001101 one byte to the right, I need to get: 01001011 00000110 10000000 Now I know how to figure out how many bytes to add onto the end of the array, but how do I shift the bits across bytes, as opposed to just lopping them off? Lee Mar 14 '06 #2

 P: n/a "Lee Crabtree" wrote in message news:eX**************@TK2MSFTNGP11.phx.gbl...I need to shift all of the values in a byte array by more than 8 bits,meaning that values should flow from one byte to another. Since I don'tknow in advance how many bits will be shifting, I can't do something easylike putting the bytes into a long or uint and shifting that. Let me givean example: If I want to shift this: 10010110 00001101 one byte to the right, I need to get: 01001011 00000110 10000000 Now I know how to figure out how many bytes to add onto the end of the array, but how do I shift the bits across bytes, as opposed to just lopping them off? int byteShift = shift/8; int bitShift = shift%8; shift whole bytes in the array by byteShift then go back and do the extra bitShift by using an intermediary UInt16 (Beware of rounding - you probably need to round up byteShift) Mar 15 '06 #3