Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if address is word aligned

Can you tell by looking at them which of these addresses is word aligned?

0x000AE430
0X00014432
0X000B0737
0X0E0D8844

like image 731
user474325 Avatar asked Dec 07 '22 01:12

user474325


1 Answers

The short answer is, yes. But you have to define the number of bytes per word. Some architectures call two bytes a word, and four bytes a double word. In any case, you simply mentally calculate addr%word_size or addr&(word_size - 1), and see if it is zero. When the address is hexadecimal, it is trivial: just look at the rightmost digit, and see if it is divisible by word size.

For a word size of 4 bytes, second and third addresses of your examples are unaligned. Second has 2 and third one has a 7, neither of which are divisible by 4. For a word size of 2 bytes, only third address is unaligned.

like image 99
vhallac Avatar answered Jan 30 '23 23:01

vhallac