#!/usr/local/bin/perl -w ############################################################################### # Creates HTML chart of powers of two, intended to be my document # http://eies.njit.edu/~walsh/powers/ # # This is a brute-force solution, but it only took an hour to implement # and it does the job. # # 04-Feb-1999 /KJW # # COPYRIGHT (c) 1999 Kevin J. Walsh ############################################################################### require 5.002; $max_power=2; $max_power=100; #$max_power=20; $headerfile="header.html"; $trailerfile="trailer.html"; ############################################################################### # ############################################################################### sub knuth { my($iscalled)=$_[0]; $iscalled=~s#/# / #g; $iscalled=~s/ / /g; return("$iscalled
\n"); } ############################################################################### # Return string corresponding to arbitrarly long integer with # nicely spaced commas of a given base. ############################################################################### sub nice_dec { return(&nice_notation($_[0], 10, 3, "" )); } sub nice_hex { return(&nice_notation($_[0], 16, 4, "x ")); } sub nice_oct { return(&nice_notation($_[0], 8, 3, "o ")); } sub nice_notation { my($x, $base, $commas, $prefix) = @_; my($digit) = ''; $_ = ''; while ( $x > 0 ) { $digit = `echo "$x % $base" | /usr/bin/bc`; $digit =~ s/\s+$//; $x = `echo "( $x - $digit ) / $base" | /usr/bin/bc`; $x =~ s/\s+$//; $_ = $hex[$digit] . $_; # Hex good for decimal or oct! } 1 while s/(\w)(\w{$commas,$commas})(?!\w)/$1, $2/g; # Perl 5.02 or better return("$prefix$_"); } ############################################################################### # ############################################################################### sub newstd { my($iscalled)=$_[0]; $iscalled=~s#/# / #g; $iscalled=~s/ / /g; return("$iscalled
\n"); } ############################################################################### # Does babble section. ############################################################################### sub notes { if ( defined( $note[$POW] ) ) { return("$note[$POW]"); } return(" "); } ############################################################################### # Write one table line. ############################################################################### sub out { #print join(' | ', @_) . "\n"; print " 2$_[0] $_[1] $_[2] $_[3] $_[4]\n"; } ############################################################################### # Don't really wanna show every single value... just interesting ones. ############################################################################### sub skip { return(1) if $POW < 40; } ############################################################################### # Mainline starts ############################################################################### $start_time = time(); $ttt='two_to_the_'; $bits="
(Only when counting " . "bits)"; $note[0]="Bit$bits"; $note[2]="Nibble$bits"; $note[3]="Byte$bits"; $note[4]=&knuth("Wyde") ."Halfword$bits"; $note[5]="Word$bits"; $note[6]="Double
Double-word$bits"; $note[7]="Quad
Quad-word$bits"; $note[10]=&newstd("Kilo-Binary-Byte/Kibibyte/KIb") .&knuth("Large Kilobyte/KKB") ."K
Kilobyte
K-byte"; $jump{'10'}="K/KB/KIb/KKB"; $note[20]=&newstd("Mega-Binary-Byte/Mebibyte/MIb") .&knuth("Large Megabyte/MMB") .&knuth("Maybe-byte") ."MB
Megabyte
Megs"; $jump{'20'}="M/MB/MIb/MMB"; $note[30]=&newstd("Giga-Binary-Byte/Gibibyte/GIb") .&knuth("Large Gigabyte/GGB") ."GB
Gigabyte
Gigs
1,024 MB"; $jump{'30'}="G/GB/GIb/GGB"; $note[40]=&newstd("Tera-Binary-Byte/Tebibyte/TIb") .&knuth("Large Terabyte/TTB") ."TB
Terabyte" . "
1,048,576 MB" . "
1,024 GB"; $jump{'40'}="T/TB/TIb/TTB"; $note[50]=&newstd("Peta-Binary-Byte/Pebibyte/PIb") .&knuth("Large Petabyte/PPB") ."PB
Petabyte

1,024 TB" . "
1,048,576 GB" . "
1,024 TB"; $jump{'50'}="P/PB/PIb/PPB"; $note[60]=&newstd("Exa-Binary-Byte/Exbibyte/EIb") .&knuth("Large Exabyte/EEB") ."EB
Exabyte

1,024 PB" . "
1,048,576 TB" . "
1,024 PB"; $jump{'60'}="E/EB/EIb/EEB"; $note[70]=&newstd("Zetta-Binary-Byte/Zebibyte/ZIb") .&knuth("Large Zettabyte/ZZB") ."Zettabyte"; $jump{'70'}="Z/ZB/ZIb/ZZB"; $note[80]=&newstd("Yotta-Binary-Byte/Yobibyte/YIb") .&knuth("Large Yottabyte/YYB") ."Yottabyte"; $jump{'80'}="Y/YB/YIb/YYB"; $note[90]="Way more than \"a lot\"" . "
Please email" . " if you know real term"; $note[100]='A #!%$ load more ' . 'than "a lot"' . "
Please email" . " if you know real term"; @hex = split(//,'0123456789ABCDEF'); #print "Powers of Two\n"; #print "\n"; #print "

Powers of Two

"; #print "\n"; #print "
More info\n"; #print "
Credits

\n"; if ( -f $headerfile ) { @header = `cat $headerfile`; foreach $x ( @header ) { print $x; } } else { print STDERR "Missing \"$headerfile\"\n"; exit(1); } @jumps=(); foreach $POW ( keys(%jump) ) { $_ = $jump{$POW}; s#/#
#g; @jumps=(@jumps, "\t\t$_\n"); } $jumpcount = $#jumps+1; print "\n"; print "\t\n\t\n"; foreach $jump ( @jumps ) { print $jump; } print "\t
Jump to
\n"; print "\n"; print "\n"; $known_power[0] = 1; # That's 2^0 $known_power[1] = 2; # That's 2^1 for($POW=0; $POW <= $max_power; $POW++ ) { $pPOW=$POW - 1; if ( defined( $known_power[$POW] ) ) { $val = $known_power[$POW]; } elsif ( defined( $known_power[$pPOW] ) ) { #print STDERR "\$known_power[$pPOW] = {$known_power[$pPOW]}\n"; $val = $known_power[$POW-1]; $val = `echo "$val * 2" | /usr/bin/bc`; } else # We'll probably never get here. { $val = `echo "2 ^ $POW" | /usr/bin/bc`; print STDERR "Forgot 2^\$POW-1??\n"; exit(1); } $val =~ s/\s+$//; $known_power[$POW] = $val; #print STDERR "\$known_power[$POW] = {$known_power[$POW]} (NEW)\n"; if ( $val eq '' ) { print STDERR "Error in bc?\n"; exit(1); } &out($POW, &nice_dec($val), &nice_hex($val), &nice_oct($val), ¬es()); if ( $POW == 19 ) { $val=1000000; out("19.93...", &nice_dec($val), &nice_hex($val), &nice_oct($val), "Mungedabyte"); $val=1024000; out("19.96...", &nice_dec($val), &nice_hex($val), &nice_oct($val), "Moronabyte"); } } $run_time = time() - $start_time; $run_time = &nice_dec($run_time); print "
Power
of Two
Decimal
Value
Hexidecimal
Value
Octal
Value
Technobabble
Jargon

This document was generated in $run_time real-time seconds with powers.pl.\n
"; if ( -f $trailerfile ) { @trailer = `cat $trailerfile`; foreach $x ( @trailer ) { print $x; } } else { print STDERR "Missing \"$trailerfile\"\n"; exit(1); }