Skip to main content

Bitwise

cameligo

val and : 'a -> nat -> nat

jsligo

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 = 2n & 1n;
let two_bytes : bytes = 0x11 & 0x10

Input

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

Output

0x10
cameligo

val or : nat -> nat -> nat

jsligo

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 = 4n | 1n;
let three_bytes : bytes = 0x11 | 0x10

Input

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

Output

0x11
cameligo

val xor : nat -> nat -> nat

jsligo

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 = 2n ^ 1n;
let one_byte : bytes = 0x11 ^ 0x10

Input

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

Output

0x01
cameligo

val shift_left : nat -> nat -> nat

jsligo

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 = 2n << 1n
let five_one_two : bytes = 0x0100 << 1n

Input

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

Output

0x000200
cameligo

val shift_right : nat -> nat -> nat

jsligo

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 = 2n >> 1n;
let zero_bytes : bytes = 0x01 >> 1n

Input

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

Output

0x00