Random Stuff

The shower is a great place for just letting your mind wander. The WWW is a great place for wandering as well. In fact, let’s face it, wandering is fun. This page is a collection of random things I have wandered across, either on the web or just by thinking things out.

Fun with hexadecimal

Before modern l33t speak, there was hexspeak, used by programmers to generally identify specific bits of data or otherwise mark memory in ways that were easily identified. Because hexadecimal notation (base 16) uses not only the digits 0-9 but also the letters A-F, there was now the opportunity to spell out works with your numbers. Since each hex digit represents 4 bits, and 32-bit values are common, you get 8 digits to play with.

Here are some common examples:


Kilos and Bits and Bits and Bits…

So, when is a kilobyte not a kilobyte?

For years, computer scientists and marketing types have abused the metric system when it comes to quantifying things like computer memory, disk space, etc. The reason for this is that in general computers are binary systems, and they work with powers of two. For the most part, this works fine, but once you start counting high enough, it becomes cumbersome to say that your computer has 1,073,741,824 (230) bytes of memory in it. So, it became common practice to shorten things by using the metric prefixes “kilo”, “mega”, “giga”, “tera”, etc.

It all started innocuously enough: 210 = 1024, which is close enough to 1000 that people started calling it a “kilo”something. So 1024 bytes became a kilobyte. Now, 24 parts out of 1000 might not seem like a big discrepancy at first, but when you take it to the next step, the problems start to occur.

In the metric system “kilo” means one thousand (103) and “mega” means one million (106), or one thousand thousands. These are supposed to be hard definitions, not subject to reality distortion fields. Having already abused the “kilo” prefix, what is a megabyte? Is it one million bytes (1,000,000)? Is it one thousand kilobytes (1,024,000)? Or is it (210)2 = 220 bytes (1,048,576)? It turns out that it all depends on whom you ask.

If you ask memory manufacturers, they will tell you that a megabyte is 220 bytes, so when you buy 256MB of RAM, you are getting 256 x 220 bytes of RAM, or 268,435,456 bytes.

On the other hand, if you ask a hard drive manufacturer, s/he will say that a megabyte is 106 bytes, so a 750MB hard drive will have 750,000,000 bytes, give or take a few.

And on the third hand, if you ask a floppy drive manufacturer, s/he will say that a megabyte is one thousand kilobytes, so a 1.44MB floppy disk will have 1,474,560 bytes, give or take a few.

It’s this sort of confusion that has led to the introduction of new binary prefixes. Thus, the metric system tries to take back ownership of “kilo” meaning 103 and “mega” meaning 106. The new binary prefixes are now “kibi” (meaning 210) and “mebi” (meaning 220). The floppy guys are pretty much left out in the cold for their twist on things!

So, the next time someone asks how much memory you have in your computer, you can proudly state that you have 256 mebibytes! And, of course, they will stare at you blankly and say “What the hell are you talking about?” Sadly, I think the chances of these prefixes catching on are small at best.

Fun With Powers of 2

If you read the discussion on kilobytes versus kibibytes, you may have noticed something interesting:

Power of 2 Value Prefix Nearest Power of 10 Value Prefix
210 1,024 kibi 103 1,000 kilo
220 1,048,576 mebi 106 1,000,000 mega
230 1,073,741,824 gibi 109 1,000,000,000 giga
240 1,099,511,627,776 tebi 1012 1,000,000,000,000 tera
250 1,125,899,906,842,624 pebi 1015 1,000,000,000,000,000 peta
260 1,152,921,504,606,846,976 exbi 1018 1,000,000,000,000,000,000 exa

If you’ll notice, for every 10 powers of two, a value increases by approximately 3 powers of 10. Thus, it’s really easy to estimate how big any power of 2 is, assuming you know the first 10 values.

For example, a 32-bit number holds 232 possible values. 232 = 22 x 230 which is approximately 22 billion or 4 billion.

Another example: a 64-bit processor can access 264 bytes of memory. 264 = 24 x 260 which is 24 exbibytes or 16 exbibytes.

If It Ain’t Broke…

If you dig up some old documentation on the 8080, Intel’s breakthrough 8-bit processor, you’ll see that the flags register is layed out like this:

 S  Z  –  H  –  V  N  C


S = sign bit (postive/negative result)
Z = zero bit (result of last operation was zero)
H = half carry bit (carry out of low 4 bits during an ADD/SUB)
C = carry bit (overflow from unsigned math operations)

Now, if you grab the latest pentium 4 manuals from Intel, and look at how the flags register is defined there, you may find it a little surprising to see that the low 8 bits of the flags looks like this:

 S  Z  –  A  –  P  –  C


S = sign bit (postive/negative result)
Z = zero bit (result of last operation was zero)
A = auxilliary carry bit (carry out of low 4 bits during an ADD/SUB)
C = carry bit (overflow from unsigned math operations)

Since the Z80 is derived from the 8080, it turns out that you can write a nicely optimized Z80 or 8080 emulator on an Intel chip, by taking advantage of the fact that 4 of the most common flags map directly onto the current P4!