Bitwise Operations: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
(Add aggregate page explaining all of the logical operations)
 
(Fixed formatting for "Bitwise or" operator cell by replacing the pipe character with its HTML code.)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
Bitwise Operations exist in programming languages as a direct way to modify the binary values, or bits, in a number. Sometimes, it's more efficient or useful to modify the bits of a number rather than performing arithmetic on the number. One example of this is a bitmask where you use the BAND operation to choose several bits to check and ignore the others.
Bitwise Operations exist in programming languages as a direct way to modify the binary values, or bits, in a number. Sometimes, it's more efficient or useful to modify the bits of a number rather than performing arithmetic on the number. One example of this is a bitmask where you use the BAND operation to choose several bits to check and ignore the others.


Pico8 has a collection of useful bitwise operations: Keep in mind that, since update 0.2.0 when they were added, the operator forms of the bitwise functions run faster in the simulated CPU than the API Calls do.
Pico8 has a collection of useful bitwise operations. Keep in mind that, since update 0.2.0 when they were added, the operator forms of the bitwise functions run faster in the simulated CPU than the API Calls do.
{| class="article-table"
{| class="article-table"
!Operation Name
!Operation Name
Line 9: Line 9:
|-
|-
|Bitwise Not
|Bitwise Not
|[[Bnot|BNOT(A)]]
|[[Bnot|<code>bnot(a)</code>]]
|~ A
|<code>~a</code>
|Inverts all of the bits of the number. 0 swaps to 1 and 1 swaps to 0. (Invert)
|Inverts all of the bits of the number. 0 swaps to 1 and 1 swaps to 0. (Invert)
|-
|-
|Bitwise And
|Bitwise And
|[[Band|BAND(A,B)]]
|[[Band|<code>band(a,b)</code>]]
|A & B
|<code>a & b</code>
|Compares the numbers at each bit position. If the bits are both 1, the result is 1, otherwise if a 1 is compared to a 0, the 0 replaces it.. (Zero-dominant)
|Compares the numbers at each bit position. If the bits are both 1, the result is 1, otherwise if a 1 is compared to a 0, the 0 replaces it.. (Zero-dominant)
|-
|-
|Bitwise Or
|Bitwise Or
|[[Bor|BOR(A,B)]]
|[[Bor|<code>bor(a,b)</code>]]
|<nowiki>A | B</nowiki>
|<code>a &#124; b</code>
|Compares the numbers at each bit position. If the bits are both 0, the result is 0, but if a 0 is compared to a 1, the 1 replaces it. (One-dominant)
|Compares the numbers at each bit position. If the bits are both 0, the result is 0, but if a 0 is compared to a 1, the 1 replaces it. (One-dominant)
|-
|-
|Bitwise Exclusive Or
|Bitwise Exclusive&nbsp;Or
|[[Bxor|BXOR(A,B)]]
|[[Bxor|<code>bxor(a,b)</code>]]
|A ^^ B
|<code>a ^^ b</code>
|Compares the numbers at each bit position. If the bits are the same, the result for that position is a 0, but if a 0 is compared to a 1, the bit is "Exclusively" in only one input, and the result is a 1. (There can be Exclusively One)
|Compares the numbers at each bit position. If the bits are the same, the result for that position is a 0, but if a 0 is compared to a 1, the bit is "Exclusively" in only one input, and the result is a 1. (There can be Exclusively One)
|-
|-
|Arithmetic Shift Left
|Shift Left
|[[Shl|SHL(A,B)]]
|[[Shl|<code>shl(a,b)</code>]]
|A << B
|<code>a << b</code>
|Shifts each bit of a number left by B positions.  
|Shifts each bit of a number left by '''b''' positions.  
|-
|-
|Arithmetic Shift Right
|Arithmetic Shift Right
|[[Shr|SHR(A,B)]]
|[[Shr|<code>shr(a,b)</code>]]
|A >> B
|<code>a >> b</code>
|Shifts each bit of a number right by B positions. Arithmetic shift attempts to preserve the sign (Positive or negative) of the number by copying the sign bit (farthest left) into the resulting empty positions.  
|Shifts each bit of a number right by '''b''' positions. Arithmetic shift attempts to preserve the sign (Positive or negative) of the number by copying the sign bit (farthest left) into the resulting empty positions.  
|-
|-
|Logical Shift Right
|Logical Shift Right
|[[Lshr|LSHR(A,B)]]
|[[Lshr|<code>lshr(a,b)</code>]]
|A >>> B
|<code>a >>> b</code>
|Shifts each bit of a number right by B positions. Logical shift ignores the sign of a number, filling zeroes into the space left over by the shift.
|Shifts each bit of a number right by '''b''' positions. Logical shift ignores the sign of a number, filling zeroes into the space left over by the shift.
|-
|-
|Rotate Left
|Rotate Left
|[[Rotl|ROTL(A,B)]]
|[[Rotl|<code>rotl(a,b)</code>]]
|A <<> B
|<code>a <<> b</code>
|Rotates each bit of a number left by B positions. In a rotation, bits which "fall off" on the left-hand side are wrapped around to the right of the number.
|Rotates each bit of a number left by '''b''' positions. In a rotation, bits which "fall off" on the left-hand side are wrapped around to the right of the number.
|-
|-
|Rotate Right
|Rotate Right
|[[Rotr|ROTR(A,B)]]
|[[Rotr|<code>rotr(a,b)</code>]]
|A >>< B
|<code>a >>< b</code>
|Rotates each bit of a number right by B positions. In the rotation, bits which "fall off" on the right-hand side are wrapped around to the left of the number.
|Rotates each bit of a number right by '''b''' positions. In the rotation, bits which "fall off" on the right-hand side are wrapped around to the left of the number.
|}
|}
== See also ==
* [[Math]]
* [[APIReference]]
[[Category:Reference]]
[[Category:API]]

Latest revision as of 03:08, 24 February 2021

Bitwise Operations exist in programming languages as a direct way to modify the binary values, or bits, in a number. Sometimes, it's more efficient or useful to modify the bits of a number rather than performing arithmetic on the number. One example of this is a bitmask where you use the BAND operation to choose several bits to check and ignore the others.

Pico8 has a collection of useful bitwise operations. Keep in mind that, since update 0.2.0 when they were added, the operator forms of the bitwise functions run faster in the simulated CPU than the API Calls do.

Operation Name API Call Operator Description
Bitwise Not bnot(a) ~a Inverts all of the bits of the number. 0 swaps to 1 and 1 swaps to 0. (Invert)
Bitwise And band(a,b) a & b Compares the numbers at each bit position. If the bits are both 1, the result is 1, otherwise if a 1 is compared to a 0, the 0 replaces it.. (Zero-dominant)
Bitwise Or bor(a,b) a | b Compares the numbers at each bit position. If the bits are both 0, the result is 0, but if a 0 is compared to a 1, the 1 replaces it. (One-dominant)
Bitwise Exclusive Or bxor(a,b) a ^^ b Compares the numbers at each bit position. If the bits are the same, the result for that position is a 0, but if a 0 is compared to a 1, the bit is "Exclusively" in only one input, and the result is a 1. (There can be Exclusively One)
Shift Left shl(a,b) a << b Shifts each bit of a number left by b positions.
Arithmetic Shift Right shr(a,b) a >> b Shifts each bit of a number right by b positions. Arithmetic shift attempts to preserve the sign (Positive or negative) of the number by copying the sign bit (farthest left) into the resulting empty positions.
Logical Shift Right lshr(a,b) a >>> b Shifts each bit of a number right by b positions. Logical shift ignores the sign of a number, filling zeroes into the space left over by the shift.
Rotate Left rotl(a,b) a <<> b Rotates each bit of a number left by b positions. In a rotation, bits which "fall off" on the left-hand side are wrapped around to the right of the number.
Rotate Right rotr(a,b) a >>< b Rotates each bit of a number right by b positions. In the rotation, bits which "fall off" on the right-hand side are wrapped around to the left of the number.

See also