Developer Blog
Articles about Using Microsoft Developer Tools

What are Kilobytes, Megabytes, Gigabytes, Etc?

Friday, April 03, 2009 5:16 AM by jonwood

Okay, we are all familiar with kilobytes, megabytes, gigabytes and the like. Most of us know that a kilobyte is approximately a thousand bytes. And that is accurate enough for most purposes. But what if you need to need to deal with these numbers a bit more accurately?

A kilobyte is exactly 1,024 bytes. So why not 1,000 bytes? Computers use binary to generate numbers. So memory addresses are naturally a power of two. 1,000 is not a power-of-two number, but 1,024 is.

Just as a million is a thousand times a thousand, a megabyte is a kilobyte times a kilobyte, or 1,048,576 bytes. Again, close enough to a million for many purposes but not exactly a million.

Here are the values for some power-of-two numbers.

Number Abbreviation Value
1 Kilobyte KB 1,024 Bytes
1 Megabyte MB 1,048,576 Bytes
1 Gigabyte GB 1,073,741,824 Bytes
1 Terabyte TB 1,099,511,627,776 Bytes
1 Petabyte PB 1,125,899,906,842,624 Bytes
1 Exabyte EB 1,152,921,504,606,846,976 Bytes

    I was working on an MFC application recently and needed to define some large numbers that I felt would be more efficient if the numbers were a power of two. I ended up defining the following C++ macros.

    #define KB(n) (((UINT64)0x400)*((UINT64)(n))) 
    
    #define MB(n) (((UINT64)0x100000)*((UINT64)(n))) 
    
    #define GB(n) (((UINT64)0x40000000)*((UINT64)(n))) 
    
    #define TB(n) (((UINT64)0x10000000000)*((UINT64)(n))) 
    
    #define PB(n) (((UINT64)0x4000000000000)*((UINT64)(n))) 
    
    #define EB(n) (((UINT64)0x1000000000000000)*((UINT64)(n)))

    Macros to make it easy to declare large power-of-two numbers.

    These macros make it easy to exactly declare a large number. For example, if I want to define four gigabytes, I can simply say GB(4).

    Nothing complicated here. But if you need to make use of these numbers, it’s nice to have a table of actual values along with some handy helper macros.

    Be the first to rate this post

    • Currently 0/5 Stars.
    • 1
    • 2
    • 3
    • 4
    • 5
    Tags:  
    Categories:   General | C++/MFC
    Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed