diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b7f3989..870278d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Optimize compare and equal functions. https://github.com/rescript-association/rescript-core/pull/238 - Add `make` and `done` + `value` functions to `AsyncIterator`. https://github.com/rescript-association/rescript-core/pull/243 +- Int: Add bitwise functions from Pervasives. https://github.com/rescript-association/rescript-core/pull/245 ## 1.5.2 diff --git a/src/Core__Int.mjs b/src/Core__Int.mjs index 7900b26f..0cf704a2 100644 --- a/src/Core__Int.mjs +++ b/src/Core__Int.mjs @@ -64,6 +64,14 @@ function clamp(min, max, value) { } } +function lnot(x) { + return x ^ -1; +} + +var Bitwise = { + lnot: lnot +}; + var Constants = { minValue: -2147483648, maxValue: 2147483647 @@ -75,5 +83,6 @@ export { range , rangeWithOptions , clamp , + Bitwise , } /* No side effect */ diff --git a/src/Core__Int.res b/src/Core__Int.res index b1a0f22e..462af4a7 100644 --- a/src/Core__Int.res +++ b/src/Core__Int.res @@ -90,3 +90,15 @@ let clamp = (~min=?, ~max=?, value): int => { | _ => value } } + +module Bitwise = { + external land: (int, int) => int = "%andint" + external lor: (int, int) => int = "%orint" + external lxor: (int, int) => int = "%xorint" + + external lsl: (int, int) => int = "%lslint" + external lsr: (int, int) => int = "%lsrint" + external asr: (int, int) => int = "%asrint" + + let lnot = x => lxor(x, -1) +} diff --git a/src/Core__Int.resi b/src/Core__Int.resi index bb74f75a..bab41ce0 100644 --- a/src/Core__Int.resi +++ b/src/Core__Int.resi @@ -387,3 +387,82 @@ Int.clamp(42, ~min=50, ~max=40) == 50 ``` */ let clamp: (~min: int=?, ~max: int=?, int) => int + +module Bitwise: { + /** + `land(n1, n2)` calculates the bitwise logical AND of two integers. + + ## Examples + + ```rescript + Int.Bitwise.land(7, 4) == 4 + ``` + */ + external land: (int, int) => int = "%andint" + + /** + `lor(n1, n2)` calculates the bitwise logical OR of two integers. + + ## Examples + + ```rescript + Int.Bitwise.lor(7, 4) == 7 + ``` + */ + external lor: (int, int) => int = "%orint" + + /** + `lxor(n1, n2)` calculates the bitwise logical XOR of two integers. + + ## Examples + + ```rescript + Int.Bitwise.lxor(7, 4) == 3 + ``` + */ + external lxor: (int, int) => int = "%xorint" + + /** + `lnot(n)` calculates the bitwise logical NOT of an integer. + + ## Examples + + ```rescript + Int.Bitwise.lnot(2) == -3 + ``` + */ + let lnot: int => int + + /** + `lsl(n, length)` calculates the bitwise logical left shift of an integer `n` by `length`. + + ## Examples + + ```rescript + Int.Bitwise.lsl(4, 1) == 8 + ``` + */ + external lsl: (int, int) => int = "%lslint" + + /** + `lsr(n, length)` calculates the bitwise logical right shift of an integer `n` by `length`. + + ## Examples + + ```rescript + Int.Bitwise.lsr(8, 1) == 4 + ``` + */ + external lsr: (int, int) => int = "%lsrint" + + /** + `asr(n, length)` calculates the bitwise arithmetic right shift of an integer `n` by `length`. + + ## Examples + + ```rescript + Int.Bitwise.asr(4, 1) == 2 + ``` + */ + external asr: (int, int) => int = "%asrint" +}