tags:

views:

245

answers:

2

Hi there,

How do you work out the alignment of an address by just looking at it?

On a 32bit system, an address of 0x12345670 means it's 16 byte aligned (because of the 0 on the end) right?

So what about:

0x12345671 0x12345672 0x12345673

etc?

Cheers, Jon

+6  A: 

An address A is n-aligned if A mod n is 0. So 0x12345671 and 0x12345673 are 1 aligned, and 0x12345672 is 2-aligned. 0x12345670 is, simultaneously, 1-aligned, 2-aligned, 4-aligned, 8-aligned, and 16-aligned. You only mention the highest power of two (as the lower powers are implied).

Martin v. Löwis
+2  A: 

You look at the ending hex digits:

  • 1, 3, 5, 7, 9, B, D, F: 1-aligned
  • 2, 6, A, E: 2-aligned
  • 4, B: 4-aligned
  • 8: 8-aligned
  • 10, 30, 50, 70, 90, B0, D0, F0: 16-aligned
  • 20, 60, A0, E0; 32-aligned
  • 40, B0: 64-aligned
  • 80: 128-aligned
  • 00: 256-aligned or more...
AProgrammer
10, 30, 50, 70, 90, B0, D0, F0: 16-aligned Isn't 0x12345620 16-aligned as well? 0x12345620 % 16 is 0.
@Jon, but it 0x12345620 % 32 is also 0, so you give the highest one (I completed Martin's answer by a practical mean)
AProgrammer
Got it. Thanks a lot.