Skip to main content

Bitwise

val and : 'a -> nat -> nat

let and : (a: 'a, b: nat) => nat

'a can either be an int or nat.

It can also be used with bytes. But the signature might be a bit different.

val and : bytes -> bytes -> bytes

A bitwise and operation.

let zero : nat = Bitwise.and 2n 1n
let two_bytes : bytes = Bitwise.and 0x11 0x10

Input

❯ ligo compile expression cameligo "Bitwise.and 0x11 0x10"

Output

0x10
let zero: nat = 2n & 1n;
let two_bytes : bytes = 0x11 & 0x10

Input

❯ ligo compile expression jsligo "0x11 & 0x10"

Output

0x10

val or : nat -> nat -> nat

let or: (a: nat, b: nat) => nat

A bitwise or operation.

It can also be used with bytes. But the signature might be a bit different.

val or : bytes -> bytes -> bytes

let five : nat = Bitwise.or 4n 1n
let three_bytes : bytes = Bitwise.or 0x11 0x10

Input

❯ ligo compile expression cameligo "Bitwise.or 0x11 0x10"

Output

0x11
let five: nat = 4n | 1n;
let three_bytes : bytes = 0x11 | 0x10

Input

❯ ligo compile expression jsligo "0x11 | 0x10"

Output

0x11

val xor : nat -> nat -> nat

let xor: (a: nat, b: nat) => nat

A bitwise xor operation.

It can also be used with bytes. But the signature might be a bit different.

val xor : bytes -> bytes -> bytes

let three : nat = Bitwise.xor 2n 1n
let one_byte : bytes = Bitwise.xor 0x11 0x10

Input

❯ ligo compile expression cameligo "Bitwise.xor 0x11 0x10"

Output

0x01
let three : nat = 2n ^ 1n;
let one_byte : bytes = 0x11 ^ 0x10

Input

❯ ligo compile expression jsligo "0x11 ^ 0x10"

Output

0x01

val shift_left : nat -> nat -> nat

let shift_left: (a: nat, b: nat) => nat

A bitwise shift left operation.

It can also be used with bytes. But the signature might be a bit different.

val shift_left : bytes -> nat -> bytes

let four : nat = Bitwise.shift_left 2n 1n
let five_one_two : bytes = Bitwise.shift_left 0x0100 1n

Input

❯ ligo compile expression cameligo "Bitwise.shift_left 0x0100 1n"

Output

0x000200
let four : nat = 2n << 1n
let five_one_two : bytes = 0x0100 << 1n

Input

❯ ligo compile expression jsligo "0x0100 << 1n"

Output

0x000200

val shift_right : nat -> nat -> nat

let shift_right: (a: nat, b: nat) => nat

A bitwise shift right operation.

It can also be used with bytes. But the signature might be a bit different.

val shift_right : bytes -> nat -> bytes

let one : nat = Bitwise.shift_right 2n 1n
let zero_bytes : bytes = Bitwise.shift_right 0x01 1n

Input

❯ ligo compile expression cameligo "Bitwise.shift_right 0x01 1n"

Output

0x00
let one : nat = 2n >> 1n;
let zero_bytes : bytes = 0x01 >> 1n

Input

❯ ligo compile expression jsligo "0x01 >> 1n"

Output

0x00