bitmath simplifies many facets of interacting with file sizes in various units. Functionality includes:

  • Converting between SI and NIST prefix units (GiB to kB)
  • Converting between units of the same type (SI to SI, or NIST to NIST)
  • Basic arithmetic operations (subtracting 42KiB from 50GiB)
  • Rich comparison operations (1024 Bytes == 1KiB)
  • bitwise operations (<<, >>, &, |, ^)
  • Sorting
  • Automatic human-readable prefix selection (like in hurry.filesize)

In addition to the conversion and math operations, bitmath provides human readable representations of values which are suitable for use in interactive shells as well as larger scripts and applications. The format produced for these representations is customizable via the functionality included in stdlibs string.format.

In discussion we will refer to the NIST units primarily. I.e., instead of “megabyte” we will refer to “mebibyte”. The former is 10^3 = 1,000,000 bytes, whereas the second is 2^20 = 1,048,576 bytes. When you see file sizes or transfer rates in your web browser, most of the time what you’re really seeing are the base-2 sizes/rates.

Don’t Forget! The source for bitmath is available on GitHub.

OH! And did we mention it has 100+ unittests? Check them out for yourself.