## FANDOM

217 Pages

Superseded
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.
shr( num, bits )
Shifts the bits of a number to the right.
num
The number.

bits
The number of bits to shift.

The `shr()` 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.

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 shifting uses the entire number representation. (See examples below.)

`shr()` performs an "arithmetic shift," which means that the sign of the number is preserved. Arithmetic right shift preserves the highest bit while also shifting a copy of it to the right, which effectively preserves the sign in Two's Complement representation.

The alternative to arithmetic shift is "logical shift." See lshr().

## Superseded by >> operator Edit

The `>>` 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 `shr(a,b)` with `a>>b`.

## Examples Edit

```-- 8 = 0b00001000 binary
-- 1 = 0b00000001 binary
print(shr(8, 3))  -- 1

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

-- -1.000 = 0b1111111111111111.0 binary (two's complement)
-- -0.125 = 0b1111111111111111.111 binary
print(shr(-1,3))  -- -0.125

print(8 >> 3)     -- 1, preferred method```

## See also Edit

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