aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.mjs
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2023-09-04 13:01:40 +0100
committerLouis Pilfold <louis@lpil.uk>2023-09-04 13:38:50 +0100
commit4aca74566e861cef0f902602501bf274ba5c43f8 (patch)
tree921a3b7ffcc5cfa33b4ee52bbe726d8a0ca598e3 /src/gleam_stdlib.mjs
parent979f0d5d5bc9b149978ab88a6df64019b302d0e3 (diff)
downloadgleam_stdlib-4aca74566e861cef0f902602501bf274ba5c43f8.tar.gz
gleam_stdlib-4aca74566e861cef0f902602501bf274ba5c43f8.zip
Bitwise int functions
Diffstat (limited to 'src/gleam_stdlib.mjs')
-rw-r--r--src/gleam_stdlib.mjs29
1 files changed, 29 insertions, 0 deletions
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index a2fbaa6..fb255d9 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -752,3 +752,32 @@ function try_get_field(value, field, or_else) {
export function byte_size(string) {
return new TextEncoder().encode(string).length;
}
+
+// In Javascript bitwise operations convert numbers to a sequence of 32 bits
+// while Erlang uses arbitrary precision.
+// To get around this problem and get consistent results use BigInt and then
+// downcast the value back to a Number value.
+
+export function bitwise_and(x, y) {
+ return Number(BigInt(x) & BigInt(y));
+}
+
+export function bitwise_not(x) {
+ return Number(~BigInt(x));
+}
+
+export function bitwise_or(x, y) {
+ return Number(BigInt(x) | BigInt(y));
+}
+
+export function bitwise_exclusive_or(x, y) {
+ return Number(BigInt(x) ^ BigInt(y));
+}
+
+export function bitwise_shift_left(x, y) {
+ return Number(BigInt(x) << BigInt(y));
+}
+
+export function bitwise_shift_right(x, y) {
+ return Number(BigInt(x) >> BigInt(y));
+}