Jump to content

Why are page sizes always powers of 2?


zak100

Recommended Posts

Hi,

I was going through a solution and I found the above question with the answer:

Quote

Recall that paging is implemented by breaking up an address into a page
and offset number. It is most efficient to break the address into X page
bits and Y offset bits, rather than perform arithmetic on the address
to calculate the page number and offset. Because each bit position
represents a power of 2, splitting an address between bits results in
a page size that is a power of 2.

Is there a better answer for this? What I understand is that we are using a binary number system, that's why page sizes should be powers of 2. Somebody please guide me whether I am right or wrong?

Zulfi.

Link to comment
Share on other sites

 

16 minutes ago, zak100 said:

Is there a better answer for this? What I understand is that we are using a binary number system, that's why page sizes should be powers of 2.

Exactly. 

If you have a 32 bit address space, for example, then the total number of bytes you can address is 232.

If you use, say, 5 bits for the address within a page (the offset) and the remaining 27 bits for the page address, then you will have 227 pages each of 25 bytes. 

Link to comment
Share on other sites

In binary numeral system, to clear the least, or the most significant bits, there can be used bitwise AND operator e.g. in C/C++:

a = x & ~( 1 << y - 1 );

and to get reminder there can be used yet another bitwise AND operator e.g.

b = x & ( 1 << y - 1 );

 

In the normal circumstances you (or CPU) would have to do modulo e.g.:

a = ( x / y ) * y;

and e.g.

b = x - a * y;

Multiplication and division by arbitrary integer are slower than bitwise operators.

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.