PICO-8 Wiki
m (some time to kill = Felice pedantically fixes more PICO-8 capitalization)
(superseded by <<)
Line 1: Line 1:
  +
{{Superseded}}
 
{{ApiReference
 
{{ApiReference
 
|name=shl
 
|name=shl
Line 8: Line 9:
   
 
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.)
 
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.)
  +
  +
== Superseded by >> operator ==
  +
  +
The <code><<</code> operator added in 0.2.0 performs the same function as <code>shl()</code> and is now the recommended way to shift bits left, as it uses fewer tokens, costs fewer cycles at runtime, and runs on the real host CPU much more efficiently. Simply replace <code>shl(a,b)</code> with <code>a<<b</code>.
   
 
== Examples ==
 
== Examples ==
Line 18: Line 23:
 
-- 1.000 = 0001.0000 binary
 
-- 1.000 = 0001.0000 binary
 
print(shl(0.125, 3)) -- 1
 
print(shl(0.125, 3)) -- 1
  +
  +
print(1 << 3) -- 8, preferred method
 
</syntaxhighlight>
 
</syntaxhighlight>
   

Revision as of 04:45, 6 May 2020

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

bits
The number of bits to shift.

The shl() function takes a number and a bit count, and returns the result of shifting 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 twos complement representation for negative and positive values. Bit shifting uses the entire number representation. (See examples below.)

Superseded by >> operator

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

Examples

-- 1 = 00000001 binary
-- 8 = 00001000 binary
print(shl(1, 3))      -- 8

-- 0.125 = 0000.0010 binary
-- 1.000 = 0001.0000 binary
print(shl(0.125, 3))  -- 1

print(1 << 3)         -- 8, preferred method

See also