zak100 Posted May 7, 2020 Share Posted May 7, 2020 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 More sharing options...
Sensei Posted May 7, 2020 Share Posted May 7, 2020 Multiplication or division by 2^x can be replaced by bitwise left shift or bitwise right shift. https://en.wikipedia.org/wiki/Bitwise_operation They are extremely fast, in comparison to regular multiplication or division by arbitrary integer (not to mention floating point). 1 Link to comment Share on other sites More sharing options...
zak100 Posted May 7, 2020 Author Share Posted May 7, 2020 Hi, Thanks a lot. Zulfi. Link to comment Share on other sites More sharing options...
Strange Posted May 7, 2020 Share Posted May 7, 2020 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. 1 Link to comment Share on other sites More sharing options...
Sensei Posted May 7, 2020 Share Posted May 7, 2020 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. 1 Link to comment Share on other sites More sharing options...
Sensei Posted May 7, 2020 Share Posted May 7, 2020 2 hours ago, Sensei said: and e.g. b = x - a * y; Ah, a is already premultiplied by y.. so, correctly I should write: b = x - a; Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now