# How many bits' address is required for a computer with n bytes of memory?

How many bits of address is required (for the program counter for example) in a byte-addressed computer with 512 Mbyte RAM?

What does the formula look like?

How is this connected with the fact that 32 bits can address no more than 4 GB RAM?

• Remember that in practice it's less or sometimes more! There may be address bits that are reserved for some other purpose so you have memory range that is never accessible. You can also access more memory than this with an instruction that says "address now refer to that other 512Mb block until we swap back" Dec 10 '12 at 16:22
• @Caleb - that's why I put it as a comment rather than an answer. And for questions like "why can I only see 2Gb on my 32bit machine on windows but 64Gb on Linux" Dec 10 '12 at 18:49
• How familiar are you with the idea of "virtual memory"? Some operating systems will take space from a hard drive to make it appear there is more RAM than they may physically be. Just an idea that may be worth noting here. There is also Physical Address Extension, en.wikipedia.org/wiki/Physical_Address_Extension, that may also be worth examining. Dec 10 '12 at 22:15
• I'd say it depends whether you're addressing the memory directly or indirectly. Feb 5 '16 at 19:50
• Why did you put a bounty on an answered question from 3 years ago? With an accepted answer!? Feb 6 '16 at 5:24

You need log2(n) bits to address n bytes. For example, you can store 256 different values in an 8 bit number, so 8 bits can address 256 bytes. 210 = 1024, so you need 10 bits to address every byte in a kilobyte. Likewise, you need 20 bits to address every byte in a megabyte, and 30 bits to address every byte in a gigabyte. 232 = 4294967296, which is the number of bytes in 4 gigabytes, so you need a 32 bit address for 4 GB of memory.

• OK so for 512 Mbyte RAM the equation is `512*1024²=2^x`(?) Thanks a lot for the prompt answer. Dec 10 '12 at 16:07
• log2(512*1024*1024) = log2(512) + log2(1024) + log2(1024) = 9 + 10 + 10 = 29. Dec 10 '12 at 16:09
• Logarithm is the inverse of exponentiation. log2(2^X) == X . Hence, log2(512) == log2(2^9) == 9. As an aside, most calculators provide log10, but you can convert log10 to log2 by dividing by log10(2). E.g., Google Calculator: log(512*1024*1024) / log(2) Dec 10 '12 at 16:18

Existing answers have explained that the formula for addressing ram is 2^BITS = Addressable ram, but have not explained why.

Consider a system with 2 bits. It can address 4 bytes of ram as follows:

Byte 0: `00`
Byte 1: `01`
Byte 2: `10`
Byte 3: `11`

For each additional bit, we can address twice as much memory. E.g., add a 0 bit to each for bytes 0-3, then add a 1 bit for bytes 4-7. We address byte X by using a bit arrangement corresponding to X in binary.

You need the log (base 2) of the N bytes in order to address N bytes of RAM directly.

``````4 GB = 2^32 bytes
log_2( 2^m ) = m
``````

so

``````log_2( 2^32 ) = 32
``````

So a 32-bit address lets you directly reference 2^32 bytes (4 GB). A 64-bit address lets you directly reference 2^64 bytes (16 exabytes).

How many bits of address is required (for the program counter for example) in a byte-addressed computer with 512 Mbyte RAM?