Skip to content

Commit 98eb2e8

Browse files
Int: Add bitwise functions from Pervasives (#245)
* Int: Add bitwise functions from Pervasives * Changelog * Put bitwise functions into their own submodule
1 parent ddc4a08 commit 98eb2e8

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Optimize compare and equal functions. https://github.com/rescript-association/rescript-core/pull/238
66
- Add `make` and `done` + `value` functions to `AsyncIterator`. https://github.com/rescript-association/rescript-core/pull/243
7+
- Int: Add bitwise functions from Pervasives. https://github.com/rescript-association/rescript-core/pull/245
78

89
## 1.5.2
910

src/Core__Int.mjs

+9
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ function clamp(min, max, value) {
6464
}
6565
}
6666

67+
function lnot(x) {
68+
return x ^ -1;
69+
}
70+
71+
var Bitwise = {
72+
lnot: lnot
73+
};
74+
6775
var Constants = {
6876
minValue: -2147483648,
6977
maxValue: 2147483647
@@ -75,5 +83,6 @@ export {
7583
range ,
7684
rangeWithOptions ,
7785
clamp ,
86+
Bitwise ,
7887
}
7988
/* No side effect */

src/Core__Int.res

+12
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,15 @@ let clamp = (~min=?, ~max=?, value): int => {
9090
| _ => value
9191
}
9292
}
93+
94+
module Bitwise = {
95+
external land: (int, int) => int = "%andint"
96+
external lor: (int, int) => int = "%orint"
97+
external lxor: (int, int) => int = "%xorint"
98+
99+
external lsl: (int, int) => int = "%lslint"
100+
external lsr: (int, int) => int = "%lsrint"
101+
external asr: (int, int) => int = "%asrint"
102+
103+
let lnot = x => lxor(x, -1)
104+
}

src/Core__Int.resi

+79
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,82 @@ Int.clamp(42, ~min=50, ~max=40) == 50
387387
```
388388
*/
389389
let clamp: (~min: int=?, ~max: int=?, int) => int
390+
391+
module Bitwise: {
392+
/**
393+
`land(n1, n2)` calculates the bitwise logical AND of two integers.
394+
395+
## Examples
396+
397+
```rescript
398+
Int.Bitwise.land(7, 4) == 4
399+
```
400+
*/
401+
external land: (int, int) => int = "%andint"
402+
403+
/**
404+
`lor(n1, n2)` calculates the bitwise logical OR of two integers.
405+
406+
## Examples
407+
408+
```rescript
409+
Int.Bitwise.lor(7, 4) == 7
410+
```
411+
*/
412+
external lor: (int, int) => int = "%orint"
413+
414+
/**
415+
`lxor(n1, n2)` calculates the bitwise logical XOR of two integers.
416+
417+
## Examples
418+
419+
```rescript
420+
Int.Bitwise.lxor(7, 4) == 3
421+
```
422+
*/
423+
external lxor: (int, int) => int = "%xorint"
424+
425+
/**
426+
`lnot(n)` calculates the bitwise logical NOT of an integer.
427+
428+
## Examples
429+
430+
```rescript
431+
Int.Bitwise.lnot(2) == -3
432+
```
433+
*/
434+
let lnot: int => int
435+
436+
/**
437+
`lsl(n, length)` calculates the bitwise logical left shift of an integer `n` by `length`.
438+
439+
## Examples
440+
441+
```rescript
442+
Int.Bitwise.lsl(4, 1) == 8
443+
```
444+
*/
445+
external lsl: (int, int) => int = "%lslint"
446+
447+
/**
448+
`lsr(n, length)` calculates the bitwise logical right shift of an integer `n` by `length`.
449+
450+
## Examples
451+
452+
```rescript
453+
Int.Bitwise.lsr(8, 1) == 4
454+
```
455+
*/
456+
external lsr: (int, int) => int = "%lsrint"
457+
458+
/**
459+
`asr(n, length)` calculates the bitwise arithmetic right shift of an integer `n` by `length`.
460+
461+
## Examples
462+
463+
```rescript
464+
Int.Bitwise.asr(4, 1) == 2
465+
```
466+
*/
467+
external asr: (int, int) => int = "%asrint"
468+
}

0 commit comments

Comments
 (0)