473,511 Members | 10,974 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to shift multi bytes?

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[10];

then store some value into pData which occupies pData[8] and pData[9].
Now I want to shift these two bytes to the left for 9 bits and still
store them in pData. The data now should span from the bit0 in
pData[6] to bit1 in pData[8].

Is there a fast way to do it?

Thanks,

Cindy

Jul 11 '07 #1
3 3121
On Jul 11, 7:18 pm, Cindy <luoqing2...@yahoo.comwrote:
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[10];
then store some value into pData which occupies pData[8] and pData[9].
Now I want to shift these two bytes to the left for 9 bits and still
store them in pData. The data now should span from the bit0 in
pData[6] to bit1 in pData[8].
Is there a fast way to do it?
No.

Not a fast way, anyway, at least not portably. Basically,
you'll have to do your shifts in a larger type (at least double
the width), separate the parts which go into different bytes,
and then or the results for each byte. You'll also have to pay
attention to not overwrite values you still need if you do it in
place.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 11 '07 #2
"Cindy" <lu*********@yahoo.comwrote in message
news:11*********************@22g2000hsm.googlegrou ps.com...
>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[10];

then store some value into pData which occupies pData[8] and pData[9].
Now I want to shift these two bytes to the left for 9 bits and still
store them in pData. The data now should span from the bit0 in
pData[6] to bit1 in pData[8].

Is there a fast way to do it?
One time I toyed with a class like this I designed. It depends on your
definition of "fast" Fast in processing time? Yes. Fast in coding? Not
really.

What I basically did was look at the number of bits to shift. If it was >=
8 I would go through the bytes and move them left one (you can determine if
want bits falling off the left to land on the right or not). I would repeat
until number of bits to shift was < 8. Then I would go through and look at
the bits that would fall off with the shift, shift, or the bits that fell
off, etc...

I just did it for fun, and it wasn't that hard.
Jul 11 '07 #3
On 11 Jul, 18:18, Cindy <luoqing2...@yahoo.comwrote:
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[10];

then store some value into pData which occupies pData[8] and pData[9].
Now I want to shift these two bytes to the left for 9 bits and still
store them in pData. The data now should span from the bit0 in
pData[6] to bit1 in pData[8].

Is there a fast way to do it?

Thanks,

Cindy
could you use std::bitset (or boost::dynamic_bitset) for that?

regards

DS

Jul 12 '07 #4

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

Similar topics

388
21421
by: maniac | last post by:
Hey guys, I'm new here, just a simple question. I'm learning to Program in C, and I was recommended a book called, "Mastering C Pointers", just asking if any of you have read it, and if it's...
5
23341
by: Parahat Melayev | last post by:
I am trying to writa a multi-client & multi-threaded TCP server. There is a thread pool. Each thread in the pool will handle requests of multiple clients. But here I have a problem. I find a...
10
2580
by: Vijay Kumar R. Zanvar | last post by:
I do not know much about shift sequence(7.1.1#5). Can somebody enlighten me, giving some examples? Regards, Vijay Kumar R. Zanvar -- Calvin: Hi Mom! I'm making my own newspaper to report...
2
5729
by: Matt Sawyer | last post by:
Hi, I'm attempting to do a drag and drop operation from one listbox to another. I have my listboxes setup with SelectionMode = MultiExtended so that I can use the shift key, cntrl key, etc. to...
4
2166
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
11
4020
by: Kenneth Lantrip | last post by:
Anyone got any ideas as to how this process could be improved for speed? this is what I have... Dim j, q As Integer Dim x(16), y(16) As Byte x.CopyTo(y, 0) ' shift left circular 24 bits
4
2209
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...
11
6197
by: rossum | last post by:
I want to declare a const multi-line string inside a method, and I am having some problems using Environment.NewLine. I started out with: class foo { public void PrintStuff() { const...
17
10616
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello everyone, Wide character and multi-byte character are two popular encoding schemes on Windows. And wide character is using unicode encoding scheme. But each time I feel confused when...
0
7144
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...
1
7085
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
5671
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,...
1
5069
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4741
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3227
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3214
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
449
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.