473,549 Members | 2,948 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 3126
On Jul 11, 7:18 pm, Cindy <luoqing2...@ya hoo.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*********@gma il.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jul 11 '07 #2
"Cindy" <lu*********@ya hoo.comwrote in message
news:11******** *************@2 2g2000hsm.googl egroups.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...@ya hoo.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
21458
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 worth the $25USD. I'm just looking for a book on Pointers, because from what I've read it's one of the toughest topics to understand. thanks in...
5
23344
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 solution but it is not how it must be... i think. When threads working without sleep(1) I can't get response from server but when I put sleep(1) in...
10
2583
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 the events of our household.
2
5735
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 make multiple selections. The problem I am having is that when I use the shift key to select a range of items, I first click on one item (item gets...
4
2169
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 write the information about user name, filename upload, filesize, date and time of uploading into the log file. (The name of the log file is...
11
4030
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
2215
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 expected it to be zero (outshifted bits don't come back). The rule I determined is: whatever the shift operator S is, its real value is (S mod 8). Is...
11
6211
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 string multiline = "The first line." + Environment.NewLine +
17
10625
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 talking with another team -- codepage -- at the same time. I am more confused when I saw sometimes we need codepage parameter for wide character...
0
7520
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7446
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7470
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7809
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6041
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5088
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3480
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1936
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
763
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.