Rotl

From Pico-8 Wiki
Jump to navigation Jump to search
Superseded / Deprecated
The feature described in this article has been superseded by a newer feature. This feature still works in PICO-8, but the new feature should be used instead. See the article's "Superseded by..." section for specific details.
rotl( num, bits )
Rotates the bits of a number to the left.
num
The number.

bits
The number of bits to rotate.

The rotl() function takes a number and a bit count, and returns the result of rotating the bits of the number to the left by that count.

Numbers in PICO-8 are stored using a 32-bit fixed point format, with 16 bits for the integer portion, 16 bits for the fractional portion, and a two's complement representation for negative and positive values. Bit rotating uses the entire number representation. (See examples below.)

See rotr() to rotate bits to the right.

Superseded by <<> operator

The <<> operator added in 0.2.0 performs the same function as rotl() and is now the recommended way to rotate bits left, as it uses fewer tokens, costs fewer cycles at runtime, and runs on the real host CPU much more efficiently. Simply replace rotl(a,b) with a<<>b.

Examples

--  8 = 0b00001000 binary
-- 64 = 0b01000000 binary
print(rotl(8, 3))      -- 64

-- 0.125 = 0b0000.0010 binary
-- 1.000 = 0b0001.0000 binary
print(rotl(0.125, 3))  -- 1

-- -4096.0000 = 0b1111000000000000.0000000000000000 binary (two's complement)
--                     rotate <-- by 12 bits
--     0.0586 = 0b0000000000000000.0000111100000000 binary
print(rotl(-4096,12))  -- 0.0586

print(8 <<> 3)         -- 64, preferred method

See also