Bitwise operators

Left shift

The left-shift operator causes the bits in the left value (either a variable or a literal)to be shifted to the left by the number of positions specified by the bits in the right value.

Example

with console from std:io
var a = 20 << 2
console.out(a) //Output: 80

Right shift

The right-shift operator causes the bits in the left value (either a variable or a literal)to be shifted to the right by the number of positions specified by the bits in the right value.

Example

with console from std:io
var a = 8 >> 2
console.out(a) //Output: 2

Bitwise and

The result of the bitwise and (denoted by &) is 1 if the corresponding bits of two operands are 1. If either bit of an operand is 0, the result of corresponding bit is evaluated to 0.

Example

Bitwise or

The result of the bitwise or (denoted by |) is 1 if at least one corresponding bit of two operands is 1.

Example

Bitwise xor

The result of the bitwise xor (denoted by ^) is 1 if the corresponding bits of two operands are opposite.

Example

One's complement

The bitwise complement (denoted by ~) changes 1 to 0 and 0 to 1.

Example

Last updated