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:

deadbeef baadfeed baadf00d c0edbabe c001d00d

### 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 (2^{30}) 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: 2^{10} = 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 (10^{3}) and “mega” means one million (10^{6}), 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 (2^{10})^{2} = 2^{20} 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 2^{20} bytes, so when you buy 256MB of RAM, you are getting 256 x 2^{20} 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 10^{6} 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 10^{3} and “mega” meaning 10^{6}. The new binary prefixes are now “kibi” (meaning 2^{10}) and “mebi” (meaning 2^{20}). 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 |

2^{10} |
1,024 | kibi | 10^{3} |
1,000 | kilo |

2^{20} |
1,048,576 | mebi | 10^{6} |
1,000,000 | mega |

2^{30} |
1,073,741,824 | gibi | 10^{9} |
1,000,000,000 | giga |

2^{40} |
1,099,511,627,776 | tebi | 10^{12} |
1,000,000,000,000 | tera |

2^{50} |
1,125,899,906,842,624 | pebi | 10^{15} |
1,000,000,000,000,000 | peta |

2^{60} |
1,152,921,504,606,846,976 | exbi | 10^{18} |
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 2^{32} possible values. 2^{32} = 2^{2} x 2^{30} which is approximately 2^{2} billion or 4 billion.

Another example: a 64-bit processor can access 2^{64} bytes of memory. 2^{64} = 2^{4} x 2^{60} which is 2^{4} 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 |

Where:

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 |

Where:

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!