Bitwise operators
Bitwise operators are used to manipulate the bits of number values.
Bitwise AND &
let a : Nat8 = 10; // binary: 0000_1010
let b : Nat8 = 6; // binary: 0000_0110
let c : Nat8 = a & b; // binary: 0010 (decimal: 2)
Bitwise OR |
let a : Nat8 = 10; // binary: 0000_1010
let b : Nat8 = 6; // binary: 0000_0110
let c : Nat8 = a | b; // binary: 0000_1110 (decimal: 14)
Bitwise XOR ^
let a : Nat8 = 10; // binary: 0000_1010
let b : Nat8 = 6; // binary: 0000_0110
let c : Nat8 = a ^ b; // binary: 0000_1100 (decimal: 12)
Bitwise Shift Left <<
let a : Nat8 = 10; // binary: 0000_1010
let b : Nat8 = a << 2; // binary: 0010_1000 (decimal: 40)
Bitwise Shift Right >>
let a : Nat8 = 10; // binary: 0000_1010
let b : Nat8 = a >> 2; // binary: 0000_0010 (decimal: 2)
Bitwise Rotate Left <<>
let a : Nat8 = 10; // binary: 0000_1010
let b : Nat8 = a <<> 2; // binary: 0010_1000 (decimal: 40)
Bitwise Rotate Right <>>
let a : Nat8 = 10; // binary: 0000_1010
let b : Nat8 = a <>> 2; // binary: 1000_0010 (decimal: 130)
Wrapping Addition +%
let a : Int8 = 127; // maximum value for a 8-bit signed integer
let b : Int8 = 1;
let c : Int8 = a +% b; // wraps around to -128
Wrapping Subtraction -%
let a : Int8 = -128; // minimum value for a 8-bit signed integer
let b : Int8 = 1;
let c : Int8 = a -% b; // wraps around to 127
Wrapping Multiplication *%
let a : Nat8 = 2;
let b : Nat8 = 128;
let c : Nat8 = a *% b; // wraps around to 0
Wrapping Exponentiation **%
let a : Nat8 = 2;
let b : Nat8 = 8;
let c : Nat8 = a **% b; // wraps around to 0