General programming

Moderators: None (Apply to moderate this forum)
Number of threads: 1642
Number of posts: 2791

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Edit Report
Question on Word Alignment (Data) Posted by Newbie on 5 Oct 2000 at 4:21 AM
Hi this is a really simple question, but I'm in a rush to finish a project which just doesn't seem to want to work, so I decided to ask here. I need to know how to check if a given memory address is double-word aligned - basically as an error checking routine. I'm ok with the actual programming part, just wondering what the process/logic behind this would be. I had an idea but it didn't seem to work...so rather than embarass myself with my lack of knowledge by saying more, I just wonder if anyone here can help me at all. I can give more detail if necessary.<br>
Thank you :)


Edit Report
Re: Question on Word Alignment (Data) Posted by Darius on 8 Oct 2000 at 3:09 PM
: Hi this is a really simple question, but I'm in a rush to finish a project which just doesn't seem to want to work, so I decided to ask here. I need to know how to check if a given memory address is double-word aligned - basically as an error checking routine. I'm ok with the actual programming part, just wondering what the process/logic behind this would be. I had an idea but it didn't seem to work...so rather than embarass myself with my lack of knowledge by saying more, I just wonder if anyone here can help me at all. I can give more detail if necessary.<br>
: Thank you :)<br>
: <br>
<br>
<br>
Ok, I don't know what language to go for, but I'll just use C. <br>
<br>
The logic behind it is the address is a multiple of 4. IE address modulus 4 equals 0.<br>
<br>
Since 4 is a power of two and addresses are unsigned you can use a shortcut to speed up the code by anding the value with 3.<br>
<br>
so to check just do (in C) &var&3 which actually works out perfectly for you. It will be 0 (zero) if it is aligned (which is false) and not 0 (true) if it isn't so you can do the following...<br>
<br>
if(&var&3){<br>
//handle unaligned stuff here.<br>
}<br>
<br>
have fun.<br>
<br>



Edit Report
Thank you! Posted by Me again on 8 Oct 2000 at 5:24 PM
Thank you for your help.





 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.