Misty Programming Language:Fit Functions

A felicitous number, or fit number for short, is a signed integer that fits in 56 bits. A fit number is an integer in the range -36028797018963968 thru 36028797018963967. The fit module provides functions that act upon fit integers.

use fit

If any of these functions is given an input that is not fit or not in range, the result is null.

fit.and(first, second)

And. The result is the bitwise and of the two fit integers.

Examples:

assign result: fit.and(12, 10)   # result is 8
assign result: fit.and(16, 2)    # result is 0
assign result: fit.and(15, 3)    # result is 3
assign result: fit.and(13, 3)    # result is 1
assign result: fit.and("10", 3)  # result is null

fit.left(first, second)

Left shift. It is similar to multiplying by a power of two. Bits that fall off of the left edge are lost.

Examples:

assign result: fit.left(12, 10)    # result is 12288
assign result: fit.left(16, 2)     # result is 64
assign result: fit.left(15, 53)    # result is -9007199254740992

fit.mask(number)

Mask. The mask function is used to generate bit fields that can be used with the other functions. If the number is 0, then the result is 0. If the number is not an integer, or if it is greater than 56 or less than -56, then the result is null. If the number is positive, then a number containing that many 1 bits is generated. If the number is negative, then a number with that many (absolute) 0 bits is generated.

Examples:

fit.mask(0)     #  0                            (00000000000000)
fit.mask(1)     #  1                            (00000000000001)
fit.mask(3)     #  7                            (00000000000007)
fit.mask(8)     #  255                          (000000000000FF)
fit.mask(16)    #  65_535                       (0000000000FFFF)
fit.mask(32)    #  4_294_967_295                (000000FFFFFFFF)
fit.mask(55)    #  36_028_797_018_963_967       (7FFFFFFFFFFFFF)
fit.mask(56)    #  -1                           (FFFFFFFFFFFFFF)
fit.mask(57)    #  null
fit.mask(-1)    #  -2                           (FFFFFFFFFFFFFE)
fit.mask(-3)    #  -8                           (FFFFFFFFFFFFF8)
fit.mask(-8)    #  -256                         (FFFFFFFFFFFF00)
fit.mask(-16)   #  -65_536                      (FFFFFFFFFF0000)
fit.mask(-32)   #  -4_294_967_296               (FFFFFF00000000)
fit.mask(-55)   #  -36_028_797_018_963_968      (80000000000000)
fit.mask(-56)   #  0                            (00000000000000) 

fit.not(fit)

Not. Flip every bit. Same as fit.xor(fit, fit.mask(56)).

fit.ones(fit)

Count the total number of 1 bits.

Examples:

assign result: fit.ones(-1)    # result is 56
assign result: fit.ones(0)     # result is 0
assign result: fit.ones(8)     # result is 1
assign result: fit.ones(18)    # result is 2

fit.or(first, second)

Or. The result is the bitwise or of the two values.

Examples:

assign result: fit.or(12, 10)   # result is 14
assign result: fit.or(16, 2)    # result is 18
assign result: fit.or(15, 3)    # result is 15
assign result: fit.or(13, 3)    # result is 15

fit.reverse(first)

Reverse. Reverse the order of the bits.

Example:

assign result: fit.reverse(-36028797018963968)    # result is 1
assign result: fit.reverse(3141592653589793)      # result is 2334719610726733

Right shift with zero fill.

Examples:

assign result: fit.right(12, 10)                   # result is 0
assign result: fit.right(19, 2)                    # result is 4
assign result: fit.right(-9007199254740992, 53)    # result is 7
assign result: fit.right(-2)                       # result is 36028797018963967

fit.right_signed(first, second)

Right shift with sign fill.

Examples:

assign result: fit.right(-2, 1)                   # result is -1

fit.rotate(first, second)

Left shift with carry fill.

Examples:

assign result: fit.rotate(1, 1)                   # result is 2
assign result: fit.rotate(-2, 1)                  # result is -3

fit.xor(first, second)

Exclusive or. The result is the bitwise exclusive-or of the two integers.

Examples:

assign result: fit.xor(12, 10)    # result is 6
assign result: fit.xor(16, 2)     # result is 18
assign result: fit.xor(15, 3)     # result is 12
assign result: fit.xor(13, 3)     # result is 14
assign result: fit.xor(13.01, 3)  # result is null

fit.zeros(fit)

Leading zeros. Count the number of leading zeros.

Examples:

assign result: fit.zeros(-1)    # result is 0
assign result: fit.zeros(0)     # result is 56
assign result: fit.zeros(1)     # result is 55