![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
Shifting Bytes
If I have in my allocated space, an arbitrary value, let's say:
1001 1101 1011 0011 0000 How can I right shift, say, the third byte one over? 1001 1101 0000 1011 0011 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
What you're showing isn't a byte, but a nybble (4 bits). Depending upon your processor, you may be able to shift a memory byte, or you may have to pull the associated bytes into registers and shift there. You'll have to respect the boundaries your uP gives you. All that said, you can shift bits between locations by shifting them through the carry flag, one bit at a time. In other words, shift the src one bit right, into the carry, shift the dest one bit right, either from the carry, or shift, then set the msb according to the carry. Wash, rinse, and repeat as necessary.
In right shifts you generally have the choice to have a zero shift in, or have the sign bit replicate itself, and sometimes to have the carry shift in. You'll have to check for your specific uP. Look for shift and/or rotate instructions.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Programming Guru
![]() Join Date: Apr 2005
Posts: 1,791
Rep Power: 5
![]() |
Whoops. I rushed my example too quickly.
So shifting has to be done manually? Start from the right and keep shifting one over until you're done? I was hoping there was something more efficient. |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Well, you shift all the bits at once, just one position at a time. It isn't that inefficient. It's a binary divide by two. You could always just pick up the byte and sling it next door, then do whatever you want with its original home. If you want to work with things that don't conform to available boundaries, then you're going to get into masking and combining and all that hooraw.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#5 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 895
Rep Power: 4
![]() |
If I understand you right, and assuming you're only wanting to work within the defined boundaries (byte, word, etc, depending on processor) you might look into whether there is a rotate instruction available.
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
|
|
#6 | |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
Quote:
:beard: |
|
|
|
|
|
|
#7 |
|
Caffeinated Neural Net
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 895
Rep Power: 4
![]() |
__________________
A man's knowledge is like an expanding sphere, the surface corresponding to the boundary between the known and the unknown. As the sphere grows, so does its surface; the more a man learns, the more he realizes how much he does not know. Hence, the most ignorant man thinks he knows it all. - L. Sprague de Camp |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Weird additional bytes | zl0h | C | 2 | Nov 11th, 2006 3:07 AM |
| Ackerman's function - crash upon launch | codylee270 | C++ | 6 | Oct 19th, 2006 2:14 AM |
| Need help building number from bytes | jloyd | Other Programming Languages | 4 | Oct 18th, 2005 8:43 AM |
| about write bytes to a file | kychan | C++ | 0 | Apr 2nd, 2005 11:05 PM |
| read bytes in character array. | Princeck | C++ | 2 | Feb 25th, 2005 1:35 PM |