Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Dec 15th, 2006, 4:42 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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
Sane is offline   Reply With Quote
Old Dec 15th, 2006, 5:24 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 15th, 2006, 5:30 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 1,885
Rep Power: 5 Sane will become famous soon enough
Send a message via MSN to Sane
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.
Sane is offline   Reply With Quote
Old Dec 15th, 2006, 5:35 PM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Dec 15th, 2006, 10:01 PM   #5
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,032
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Old Dec 15th, 2006, 10:11 PM   #6
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
Quote:
Originally Posted by Post #2
Look for shift and/or rotate instructions.
:beard:
DaWei is offline   Reply With Quote
Old Dec 15th, 2006, 11:09 PM   #7
lectricpharaoh
Caffeinated Neural Net
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,032
Rep Power: 5 lectricpharaoh will become famous soon enough
Quote:
Originally Posted by DaWei View Post
:beard:
My bad. That's what I get for not sleeping, eh?
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC