473,403 Members | 2,293 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,403 software developers and data experts.

How to shift a byte-array?

Hi C# Experts:

I have a byte-array:

byte [] byte_array = new byte[5];

byte_array[0] = 0;
byte_array[1] = 1;
byte_array[2] = 2;
byte_array[3] = 3;
byte_array[4] = 4;

Is there a way to shift the contents of the array to left by 2 byte? The end
result should be:

byte_array[0] = 2;
byte_array[1] = 3;
byte_array[2] = 4;
byte_array[3] = 0; // whatever is ok
byte_array[4] = 0; // whatever is ok

Thanks in advance!
Polaris

Sep 4 '08 #1
2 13589
"Polaris" <et*******@hotmail.comwrote in message
news:36**********************************@microsof t.com...
Hi C# Experts:

I have a byte-array:

byte [] byte_array = new byte[5];

byte_array[0] = 0;
byte_array[1] = 1;
byte_array[2] = 2;
byte_array[3] = 3;
byte_array[4] = 4;

Is there a way to shift the contents of the array to left by 2 byte? The
end
result should be:

byte_array[0] = 2;
byte_array[1] = 3;
byte_array[2] = 4;
byte_array[3] = 0; // whatever is ok
byte_array[4] = 0; // whatever is ok

Thanks in advance!
Polaris
I smell Homework... But ask your professor why "whatever" is OK...and don't
give up till he/she gives a good answer.

Sep 4 '08 #2
You can just use Array.CopyTo or Buffer.BlockCopy to copy 3 bytes
starting index 2 to index 0. If "whatever is OK", the simplest
approach is to not change last two bytes, so the final array would be
{2,3,4,3,4} - but you can also use Array.Clear to clear 2 bytes
starting index 3.

Marc
Sep 4 '08 #3

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

Similar topics

10
by: Kamilche | last post by:
I'm trying to pack two characters into a single byte, and the shifting in Python has me confused. Essentially, it should be possible to use a 'packed string' format in Python, where as long as...
8
by: Chua Wen Ching | last post by:
Hi, I had some beginner questions. Do we need Shift << >> or Logical AND OR XOR operator in our daily programming? I am not sure why i need to use it? I had some samples of c# codes using it. ...
4
by: Markus Hahn | last post by:
While coding some binary data handling I found this IMHO strange behavior: Dim bToShift As Byte = 1 bToShift <<= 9 Console.WriteLine("and the value is {0}", bToShift) The output is "2", I...
7
by: Marty McFly | last post by:
Hello VB Gurus, I have an unusual requirement to shift an unsigned int right one bit: Dim myVar As UInt32 = UInt32.Parse("123456") Dim myResult As UInt32 myResult = myVar >> 1 However, the...
11
by: vj | last post by:
Hello group, I am working on a compression tool and saw this puzzling bit shit behaviour on a VC++6.0 compiler. #include <iostream> using namespace std; typedef unsigned char uchar; #define...
1
by: John Richardson | last post by:
I'm trying to override the SHIFT-SPACE "negative feature" in the Winforms datagrid, to only be a space. The following link describes this:...
12
by: Mick_fae_Glesga | last post by:
OK, the solution to this is probably blindingly obvious to everyone, but... surely it can't be right. I am compiling with borland bcc32 free compiler this piece of code is designed to identify...
7
by: yancheng.cheok | last post by:
hello all, in my memory content says, 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8 ... 0xff i wish to shift it n bits. please note that, not n bye, but n bit, and n can be any value 0, 1,...
3
by: Cindy | last post by:
I am struggling over a simple way to shift multi bytes for certain bits. Hope someone can help. For example, I open a memory space for 10 bytes: unsigned char *pData = new unsigned char; then...
11
by: Bob Altman | last post by:
Hi all, I want to write a generic class that does this: Public Class X (Of T) Public Sub Method(param As T) dim x as T = param >3 End Sub End Class
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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:
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.