How to shift multi bytes?

C

Cindy

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
 
A

Alf P. Steinbach

* Cindy:
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?

Use image support in Boost? Disclaimer: I haven't used it.
 
J

James Kanze

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

Jim Langston

Cindy said:
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.
 
D

dasjotre

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,123
Latest member
Layne6498
Top