- Shifts the bits of a number to the right, using logical shift.
- The number.
- The number of bits to shift.
lshr() function takes a number and a bit count, and returns the result of shifting the bits of the number to the right by that count. This uses a "logical shift."
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 twos complement representation for negative and positive values. Bit shifting uses the entire number representation. (See examples below.)
lshr() performs a "logical shift," which shifts all of the raw bits of the number, filling the highest bits with zeroes. The alternative to logical shift is "arithmetic shift." See shr().
Superseded by >>> operator Edit
>>> operator added in 0.2.0 performs the same function as
shr() and is now the recommended way to shift bits right, as it uses fewer tokens, costs fewer cycles at runtime, and runs on the real host CPU much more efficiently. Simply replace
-- 8 = 0b00001000 binary -- 1 = 0b00000001 binary print(lshr(8, 3)) -- 1 -- 1.000 = 0b0001.0000 binary -- 0.125 = 0b0000.0010 binary print(lshr(1, 3)) -- 0.125 -- -1.000 = 0b1111111111111111.0 binary (two's complement) -- 8191.875 = 0b0001111111111111.111 binary print(lshr(-1,3)) -- 8191.875 print(8 >>> 3) -- 1, preferred method