Bitwise operators

Currently osl has support for:\

Bitwise AND

Bitwise and for osl is identical to bitwise and in js

Bitwise OR

Bitwise or for osl is identical to bitwise or in js

Bitwise XOR (^^)

The bitwise XOR operator (^^) returns 1 for each bit position where the corresponding bits of its operands are different.

Bitwise Left Shift (<<)

The left shift operator (<<) shifts the bits of the first operand to the left by the number of positions specified by the second operand.

Bitwise Right Shift (>>)

The right shift operator (>>) shifts the bits of the first operand to the right by the number of positions specified by the second operand.

Important Notes

  1. Operator Precedence

    • Bitwise operators have lower precedence than arithmetic operators

    • Use parentheses to ensure operations are performed in the intended order

  2. Type Handling

    • Operands are converted to integers before the operation

    • Results are always integers

    • Negative numbers use two's complement representation

  3. Common Use Cases

    • Bit manipulation in flags and masks

    • Fast multiplication/division by powers of 2

    • Memory-efficient storage of boolean values

    • Data encryption and hashing algorithms

  4. Best Practices

    • Use comments to explain complex bitwise operations

    • Consider readability vs. performance when choosing between bitwise and arithmetic operations

    • Be careful with shift amounts larger than the number's bit width

Last updated

Was this helpful?