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 for specific details.
rotr( num, bits )
Rotates the bits of a number to the right.
The number.

The number of bits to rotate.

The rotr() function takes a number and a bit count, and returns the result of rotating the bits of the number to the right 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 rotl() to rotate bits to the left.

Superseded by >>< operator Edit

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

Examples Edit

-- 64 = 0b01000000 binary
--  8 = 0b00001000 binary
print(rotr(64, 3))     -- 8
-- 1.000 = 0b0001.0000 binary
-- 0.125 = 0b0000.0010 binary
print(rotr(1, 3))      -- 0.125
-- -4096.0000 = 0b1111000000000000.0000000000000000 binary (two's complement)
--                     rotate --> by 12 bits
--    15.0000 = 0b0000000000001111.0000000000000000 binary (two's complement)
print(rotr(-4096,12))  -- 15
-- (when printing fractional numbers, pico-8 rounds the decimal
-- representation to four decimal places. the largest fractional
-- portion is about 0.9999847... in decimal, so pico-8 prints it 
-- as "1".)
-- approx 1 = 0b0000000000000000.1111111111111111 binary (two's complement)
-- -15.9998 = 0b1111111111110000.0000000000001111 binary (two's complement)
print(rotr(0b0.1111111111111111, 12)  -- -15.9998
print(64 >>< 3)        -- 8, preferred method

See also Edit

Community content is available under CC-BY-SA unless otherwise noted.