bcal - Man Page

Bits, bytes and general-purpose calculator.

Synopsis

bcal [-b [expr]] [-c N] [-p N] [-f loc] [-s bytes] [expr] [N [unit]] [-m] [-H] [-d] [-h]

Description

bcal (Byte CALculator) is a command-line utility to help with calculations and expressions involving binary prefixes, SI/IEC conversion, byte addressing, base conversion, LBA/CHS calculation etc.

To evaluate general-purpose operations, use bcal -b or prompt key b.

bcal uses [SI and IEC binary prefixes]: https://en.wikipedia.org/wiki/Binary_prefix

Features

 * evaluate arithmetic expressions involving storage units
 * general-purpose operations
   - arithmetic: addition, subtraction, multiplication, division, modulo
   - bitwise: AND (&), OR (|), XOR (^), complement (~), lshift (<<), rshift (>>)
   - functions: exp(n), log(base, n), ln(n) [natural log], pow(n, exponent), root(radical, n), sum(n1 n2 ...)
 * works with piped input or file redirection
 * convert to IEC/SI standard data storage units
 * REPL mode with the last valid result stored for reuse
 * show the address in bytes
 * show address as LBA:OFFSET
 * convert CHS to LBA and vice versa
 * base conversion to binary, decimal and hex
 * show bit positions with bit value of a number
 * custom sector size, max heads/cylinder and max sectors/track
 * minimal dependencies

Operational Notes

  1. REPL mode: bcal enters the REPL mode if no arguments are provided. Storage unit conversion, base conversion and expression evaluation are supported in this mode. The last valid result is stored in the variable r.
  2. Expression: Expression passed as argument single execution mode must be quoted. Inner spaces are ignored. Operators supported in storage expressions: +, -, *, /, %.
  3. N [unit]: N can be a decimal or '0x' prefixed hex value. unit can be B/KiB/MiB/GiB/TiB/kB/MB/GB/TB. Default is Byte. As all of these tokens are unique, unit is case-insensitive. Note that bcal doesn't support bit as a unit.
  4. Numeric representation: Decimal and hex are recognized in expressions and unit conversions. Binary is also recognized in other operations.
  5. Syntax: Prefix hex inputs with '0x', binary inputs with '0b'.
  6. Precision: 128 bits if __uint128_t is available or 64 bits for numeric conversions. Floating point operations use long double. Negative values in storage expressions are unsupported. Only 64-bit operating systems are supported.
  7. Fractional bytes do not exist, because they can't be addressed. bcal shows the floor value of non-integer bytes.
  8. CHS and LBA syntax:
     - LBA: 'lLBA-MAX_HEAD-MAX_SECTOR'   [NOTE: LBA starts with 'l' (case ignored)]
     - CHS: 'cC-H-S-MAX_HEAD-MAX_SECTOR' [NOTE: CHS starts with 'c' (case ignored)]
     - Format conversion arguments must be hyphen separated.
     - Any unspecified value, including the one preceding the first '-' to the one following the last '-', is considered '0' (zero).
     - Examples:
       - 'c-50--0x12-' -> C = 0, H = 50, S = 0, MH = 0x12, MS = 0
       - 'l50-0x12' -> LBA = 50, MH = 0x12, MS = 0
  9. Default values:
     - sector size: 0x200 (512)
     - max heads per cylinder: 0x10 (16)
     - max sectors per track: 0x3f (63)
  10. REPL mode: r is synced and can be used in expressions. The built-in evaluator uses long double arithmetic.
  11. History file: Stored at $XDG_CONFIG_HOME/bcal/history, or $HOME/.config/bcal/history if XDG_CONFIG_HOME is unset.

Environment

BCAL_BIT_ANSI_COLOR_CODE
ANSI escape code to colorize bit value 1 in -p output. Set to empty to disable coloring. Examples:
    // ANSI 8-bit color code
    export BCAL_BIT_ANSI_COLOR_CODE=$'\033[1;92m'
    // ANSI 256-bit color code
    export BCAL_BIT_ANSI_COLOR_CODE=$'\u001b[1;38;5;51m'
    // Disable coloring
    export BCAL_BIT_ANSI_COLOR_CODE=''

Options

-b=[expr]

Start in general-purpose REPL mode. If expression is provided, evaluate and quit.

-c=N

Show decimal, binary and hex representation of positive integer N.

-p=N

Show bit position (reversed if set) and bit value of a number.

-f=loc

Convert CHS to LBA or LBA to CHS. loc is hyphen-separated representation of LBA or CHS.
Please refer to the Operational Notes section for more details.

-s=bytes

Sector size in bytes. Default value is 512.

-m

Show minimal output (e.g. decimal bytes).

-H

Show integral maths results in hex.

-d

Enable debug information and logs.

-h

Show program help, storage sizes on the system and exit.

Prompt Keys

b

Toggle numeric expression mode.

c N

Show decimal, binary and hex representation of positive integer N.

p N

Show bit position (reversed if set) and bit value of a number.

r

Show result from last operation.

s

Show sizes of storage types.

?

Show prompt help.

q, double Enter

Quit the program.

prompt

bytes> for storage mode, maths> for general-purpose mode.

Examples

  1. Evaluate arithmetic expression of storage units

    $ bcal "(5kb+2mb)/3"
    $ bcal "5 tb / 12"
    $ bcal "2.5mb*3"
    $ bcal "(2giB * 2) / (2kib >> 2)"
  2. Convert storage capacity to other units and get address, LBA.

    $ bcal 20140115 b
    $ bcal 0x1335053 B
    $ bcal 0xaabbcc kb
    $ bcal 0xdef Gib
    Note that the units are case-insensitive.
  3. Convert storage capacity, set sector size to 4096 to calculate LBA.

    $ bcal 0xaabbcc kb -s 4096
  4. Convert LBA to CHS.

    $ bcal -f l500
    $ bcal -f l0x600-18-0x7e
    $ bcal -f l0x300-0x12-0x7e
  5. Convert CHS to LBA.

    $ bcal -f c10-10-10
    $ bcal -f c0x10-0x10-0x10
    $ bcal -f c0x10-10-2-0x12
    $ bcal -f c-10-2-0x12
    $ bcal -f c0x10-10--0x12
  6. Show binary, decimal and hex representations of a number.

    $ bcal -c 20140115
    $ bcal -c 0b1001100110101000001010011
    $ bcal -c 0x1335053
    bytes> c 20140115 // REPL mode
    maths> c 20140115 // REPL mode
  7. Perform bitwise operations.

    $ bcal -b '0xFF & 0x0F'
    $ bcal -b '0x0F | 0xF0'
    $ bcal -b '0xFF ^ 0xF0'
    $ bcal -b '~0xF0'
    $ bcal -b '0x01 << 3'
    $ bcal -b '0x10 >> 2'
    $ bcal -b '(0xFF & 0x0F) | (0x0F << 4)'
  8. Use as a general-purpose calculator.

    $ bcal -b '3.5 * 2.1 + 5.7' // Single execution mode
    $ bcal -b // Start in general-purpose REPL mode
    $ bcal
    bytes> b // Switch to general-purpose mode
    maths> 3.5 * 2.1 + 5.7
  9. Pipe input.

    $ printf '15 kib + 15 gib \n r / 5' | bcal -m
    $ printf '15 + 15 + 2' | bcal -bm
  10. Redirect from file.

    $ cat expr
    15 gib + 15 kib
    r / 5
    $ bcal -m < expr
  11. Use mathematical functions.

    $ bcal -b 'root(2, 17.3)' // square root on 17.3
    $ bcal -b 'exp(5.2)'
    $ bcal -b 'pow(2, 8)'
    $ bcal -b 'sum(1 2 3 4)'
    $ bcal -b 'pow(10, 3) + root(2, 9)'

Authors

Arun Prakash Jana <engineerarun@gmail.com>

Home

https://github.com/jarun/bcal

Reporting Bugs

https://github.com/jarun/bcal/issues

License

Copyright © 2016 Arun Prakash Jana <engineerarun@gmail.com>

License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law.

Info

05 Sep 2026 Version 2.6