Question on Word Alignment (Data) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Question on Word Alignment (Data)

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.

Thank you :)


Comments

  • : 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.

    : Thank you :)

    :





    Ok, I don't know what language to go for, but I'll just use C.



    The logic behind it is the address is a multiple of 4. IE address modulus 4 equals 0.



    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.



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



    if(&var&3){

    //handle unaligned stuff here.

    }



    have fun.






  • Thank you for your help.


Sign In or Register to comment.