Section documentation for STACK-Maxima /basen/binary
Functions specific for working with binary stackbasen objects.
Basically, the stackbasen system does not directly understand negative
numbers nor are there general bit-wise logical operators in the system. For
these purposes we have special binary tools in this category. The common thing
for these tools is that you need to describe the number of bits in your numbers
otherwise things won't work.
Some of these tools also consider octal and hexadecimal numbers as "binary".
For example sbasen_bitwidth will accept bases 2, 8 and 16 and provide
the number of bits required for the value with its paddings. Likewise
sbasen_bitwise_eval will also act on numbers in those bases. To identify
numbers in those bases use sbasen_is_cs.
sbasen_bitwidth(expression)
Returns the maximum bitwidth of all the base 2, 8 or 16 stackbasen objects
present in the expression. Should none be found returns -1.
| Argument name | type | description |
|---|---|---|
| expression | expression | an expression to check |
| Return type | description |
|---|---|
| integer | -1 if no stackbasen numbers of the relevant bases were found |
sbasen_bitwise_eval(basen_logic)
Given a logical expression with operators not, and, or, nand, nor,
xor, or xnor of stackbasen objects. Will interpret those all as their
bitwise equivalents and evaluate operations between those and binary
representations of the stackbasen objects. Will use the bitwidth of
the widest base-2 (or 8 or 16) stackbasen object in the expression and
pads all others to that length.
The return value will be in a binary format of that bitwidth if and only if the whole expression could be evaluated. Otherwise, only parts may have been evaluated.
Will only touch parts of expression where all arguments are base-2,8,16...
This logic assumes 1 = true. And will always produce "S" format output.
To define things further, xnor is not xor(a,b,...) and xor is true
when odd number of arguments are true, not just when exactly one is.
| Argument name | type | description |
|---|---|---|
| basen_logic | expression | expression with stackbasen objects as targets of normal logical operators |
| Return type | description |
|---|---|
| expression/stackbasen | and expression where all logical operations between "binary" stackbasen-objects have been evaluated as bitwise operations. |
sbasen_convert_to_binary(number,format)
A function to convert to binary stackbasen. Will work with positive
raw integers or with any stackbasen objects.
Does not verify validity of those stackbasen objects, so might generate
interesting results.
Do note that for general conversion between bases you can simply use
the constructor sbasen_construct_from_base10 and converter
sbasen_convert_to_base10. This is just a convenience wrapper for them.
| Argument name | type | description |
|---|---|---|
| number | integer or stackbasen | a number to convert. |
| format | string | a single character string selecting between "C" and "S" presentation formats |
| Return type | description |
|---|---|
| stackbasen | an object representing that binary number. |
sbasen_is_cs(expression)
Checks if the expression contains a stackbasen value and that all
such values it contains are in traditional computer science bases, i.e,
2, 8 or 16.
| Argument name | type | description |
|---|---|---|
| expression | expression | an expression to check |
| Return type | description |
|---|---|
| bool | false if no stackbasen numbers were found or if even one of them is not in the expected bases. |
sbasen_pad(sbasen_number,width)
Pads a stackbasen number with 0-digits from the left so that the number
has the desired width (bit width or otherwise). Intended for binary
operations but can be used in general for example to pad hexadecimal to
a given word size.
Note should the number already be wider than the width will not modify it.
Use sbasen_truncate_left to throw the extra high digits away if that is
neeeded.
| Argument name | type | description |
|---|---|---|
| sbasen_number | stackbasen | a stackbasen-object to pad. |
| width | int | the digit width to aim for |
| Return type | description |
|---|---|
| stackbasen | the padded stackbasen-object. |
sbasen_trim_padding(expression)
Drops extra 0 digits from stackbasen objects that may have been padded
to match some "bit width". Works for all bases.
| Argument name | type | description |
|---|---|---|
| expression | expression | with possible stackbasen-objects to trim. |
| Return type | description |
|---|---|
| expression | expression with the stackbasen-objects without extra leading zeros. |
sbasen_truncate_left(sbasen_number,width)
Drops extra highest digits from the number if the number is overly long. Might be of use when doing binary operations.
There is a lowest digits version sbasen_truncate_right should one need it.
| Argument name | type | description |
|---|---|---|
| sbasen_number | stackbasen | a stackbasen-object to truncate. |
| width | int | the digit width to aim for |
| Return type | description |
|---|---|
| stackbasen | the padded stackbasen-object. |
sbasen_truncate_right(sbasen_number,width)
Drops extra lowest digits from the number if the number is overly long. Might be of use when doing binary operations.
There is a highest digits version sbasen_truncate_left should one need it.
| Argument name | type | description |
|---|---|---|
| number | stackbasen | a stackbasen-object to truncate. |
| width | int | the digit width to aim for |
| Return type | description |
|---|---|
| stackbasen | the padded stackbasen-object. |