Tutorial Bitwise and List Operations, A Reference

Discussion in 'Tutorials and Help' started by Raptorblaze, Mar 2, 2016.

  1. Raptorblaze

    Raptorblaze Head Coder Staff Member

    This guide is meant as a quick reference for bitwise and list operations, what they do, and some examples of their use.

    Definition, Bitwise Operation: A bitwise operation is one that works on a value with respect to it's binary bit representation, rather than decimal value. Examples will use four bits (or more if needed) however in BYOND the bit width is 16, which means there are 16 bits available for these operations.

    Bitwise Operators

    Symbol: &
    Name: AND
    Description: C = A & B, where C has a 1 in any bit position where BOTH A and B are 1. Any other position is 0.
    Example: 1101 & 0011 = 0001

    Symbol: |
    Name: OR
    Description: C = A | B, where C has a 1 in any bit position in which either A or B has a 1.
    Example: 1100 | 1001 = 1101

    Symbol: !
    Name: NOT
    Description: B = !A, where B has a 1 in any position A had a 0, and B has a 0 in any position A had a 1. (The values of the bits are flipped.)
    Example: !1101 = 0010

    Symbol: ^
    Name: XOR (Exclusive Or)
    Description: C = A ^ B, where C has a 1 in any position which was 1 in A or B, but not in both.
    Example: 1100 ^ 0110 = 1010

    Symbol: >>
    Name: Bit Shift Right
    Description: C = A >> B, where C is the bits of A, shifted to the right B times. Any bits that shift past the rightmost position are lost. (each shift is equivalent to dividing the number by 2, losing any decimals.)
    Example: 0011 0010 >> 3 = 0000 0110

    Symbol: <<
    Name: Bit Shift Left
    Description: C = A << B, where C is the bits of A, shifted to the left B times. Any bits that shift past the leftmost position are lost. (each shift is equivalent to multiplying the number by 2, losing anything beyond the 16th bit.)
    Example: 0001 1000 << 4 = 1000 0000

    Special Symbol: (operator)=
    Description: For any of the above symbols, there is also a version with a trailing '=' sign. I'll use AND as an example, but the logic works for any of the operators. This also applies to all list operators (except "in".)
    Example: A &= B, is equivalent to saying A = A & B

    List Operators

    Symbol: +
    Description: Returns a new list which consists of list A, with list B appended to it.
    Example: if A is list(4,5,6) and B is list(1,2,3,6), A+B would be list(4,5,6,1,2,3,6), notice 6 is duplicated

    Symbol: -
    Description: Returns a new list which consists of list A, with B removed from it. If B is a single item, that item is removed. If B is a list, each of B's items is removed.
    Example: if A is list(1,2,3,4,5,6) and B is list(2,4,6) then A-B = list(1,3,5)

    Symbol: |
    Description: Returns a new list which consists of all members of A and B, combining any duplicate members. Items in list A are added first, followed by any items in list B not present in A.
    Example: if A is list(4,5,6) and B is list(1,2,3,6), A|B would be list(4,5,6,1,2,3)

    Symbol: &
    Description: Returns a list which consists of all members present in BOTH A and B. The order the items were in list A is used.
    Example: A = list(1,2,3), B = list(2,3,4), A&B = list(2,3)

    Symbol: ^
    Description: Returns a list consisting of members in A or B, but not both. Items present in A come first, followed by items in B.
    Example: A = list(1,2,3), B = list(2,3,4), A^B = list(1,4)

    Symbol: in
    Description: Checks for the presence of A in list B.
    Example: if A = 4, and B = list(1,2,3,4,5,6,7), A in B is true.

Share This Page