Online or native shell calculators either have weird syntax to follow, or no syntax at all that allows nested calculations. Bmath solves these problems with a one stop tool that does calculations and conversions.
There is no support for negative values. The maximum number a calculation may produce is a unsigned 64-bit integer. Overflow is possible if an expression calculation exceeds that number. This program assumes little-endian system, but big-endian input.
Using homebrew, install the following libraries:
brew install argp-standalone
brew install readline
brew install cmake
brew install iconv
- (test suite only)
brew install criterion
- (test suite only) Install Criterion
Clone this repo, cd
into the cloned directory, and then run the following commands:
mkdir -p build && cd build && -DCMAKE_BUILD_TYPE=Release ../CMakeLists.txt
make
sudo make install
Installs to the
/usr/local/bin
directory.
Usage: bmath [-u?V] [-d EXPR] [--detached=EXPR] [--uppercase] [--unicode] [--binary]
[--help] [--usage] [--version]
Run ./bmath
to run the program in interactive mode. To exit, use ctrl + c
,
or type in "quit" or "exit".
NOTE: Bmath runs in interactive mode by default. The "-d" argument is used to run expressions one by one in non-interactive (detached) mode.
$ ./bmath -d "1 << 6"
Dec: 64
Char: @
Hex: 0x40
Hex16: 0x0040
Hex32: 0x00000040
Hex64: 0x0000000000000040
Also supports hex conversions:
$ ./bmath -d "0x40"
Dec: 64
Char: @
Hex: 0x40
Hex16: 0x0040
Hex32: 0x00000040
Hex64: 0x0000000000000040
$ ./bmath -d "0x1 | 0x2"
Dec: 3
Char: <special>
Hex: 0x3
Hex16: 0x0003
Hex32: 0x00000003
Hex64: 0x0000000000000003
When a value is calculated greater than unsigned 16-bit integer:
$ ./bmath -d "1 << 17"
Dec: 131072
Char: Exceeded
Hex: 0x20000
Hex16: Exceeded
Hex32: 0x00020000
Hex64: 0x0000000000020000
expr = expr, op, term
| term ;
term = term, shift_op, factor
| factor ;
factor = number
| lparen, expr, rparen
| negate, factor ;
number = digit, { digit }
| hex ;
digit = [0-9], { [0-9] } ;
hex = "0x", [0-9a-fA-F], { [0-9a-fA-F] } ;
shift_op = "<<" | ">>" ;
op = "^" | "|" | "&" ;
lparen = "(" ;
rparen = ")" ;
negate = "~" ;