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

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

# 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

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

# 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

1
with console from std:io
2
3
var a = 12 //00001100 in binary
4
var b = 25 //00011001 in binary
5
6
console.out(a&b) //Output: 8
Copied!

# Bitwise or

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

## Example

1
with console from std:io
2
3
var a = 12
4
var b = 25
5
6
console.out(a|b) //Output: 29
Copied!

# Bitwise xor

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

## Example

1
with console from std:io
2
3
var a = 12
4
var b = 25
5
6
console.out(a^b) //Output: 21
Copied!

# One's complement

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

## Example

1
with console from std:io
2
3
var a = 35 //00100011 in binary
4
5
console.out(~a) //Output: 220
Copied!