FANDOM


rotr( num, bits )
Rotates the bits of a number to the right.
num
The number.

bits
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.

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

See also Edit

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