xoofx/kalk

big-endian bin

Closed this issue · 1 comments

The string containing the binary representation output by bin, which can also be its input, is divided into bytes written least-significant to most-significant left to right, i.e., supposing memory locations increase left to right, in "little-endian" (LE) order. This order is the most widespread order nowadays, but to better understand the constituents of the representation the "big-endian" (BE) order is definitely more intuitive – especially for floating-point numbers. BTW, it can also be interpreted as the LE order with memory addresses increasing right to left.
I don't know what the BE equivalent of the bin function could be called, or if other mechanisms to select the BE representation could be devised, but let me call it for now the bebin function. Example calls:

>>> bebin 259
# bebin(259)
out = "00000000 00000000 00000001 00000011"
>>> bebin "00000000 00000000 00000001 00000011"
# bebin("00000000 00000000 00000001 00000011")
out = 259
>>> bebin 1.25
# bebin(1.25)
out = "00111111 11110100 00000000 00000000 00000000 00000000 00000000 00000000"
xoofx commented

I have introduced instead new functions (bin2, bin4, bin8 and hex2, hex4, hex8) in order to control the byte grouping.

For example:

>>> bin4 259
# bin4(259)
out = "00000000_00000000_00000001_00000011"
>>> bin4 out
# bin4(out)
out = 259