Skip to content

Commit 1b4600e

Browse files
docs: ✏️ bitwise functions
1 parent 7f3abab commit 1b4600e

File tree

1 file changed

+86
-10
lines changed

1 file changed

+86
-10
lines changed

packages/u64/src/bitwise.ts

Lines changed: 86 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,47 @@ function normalise(bits: number): number {
88
return bits % 64
99
}
1010

11+
/**
12+
* Performs a bitwise AND operation on two `U64` values. Equivalent to the
13+
* numeric `&` operator.
14+
*
15+
* @param a - First operand.
16+
* @param b - Second operand.
17+
*
18+
* @returns The result of the bitwise AND operation.
19+
*
20+
* @category Bitwise
21+
*/
1122
export function and(a: U64, b: U64): U64 {
1223
return [a[0] & b[0], a[1] & b[1], a[2] & b[2], a[3] & b[3]]
1324
}
1425

26+
/**
27+
* Performs a bitwise OR operation on two `U64` values. Equivalent to the
28+
* numeric `|` operator.
29+
*
30+
* @param a - First operand.
31+
* @param b - Second operand.
32+
*
33+
* @returns The result of the bitwise OR operation.
34+
*
35+
* @category Bitwise
36+
*/
1537
export function or(a: U64, b: U64): U64 {
1638
return [a[0] | b[0], a[1] | b[1], a[2] | b[2], a[3] | b[3]]
1739
}
1840

41+
/**
42+
* Performs a bitwise XOR operation on two `U64` values. Equivalent to the
43+
* numeric `^` operator.
44+
*
45+
* @param a - First operand.
46+
* @param b - Second operand.
47+
*
48+
* @returns The result of the bitwise XOR operation.
49+
*
50+
* @category Bitwise
51+
*/
1952
export function xor(a: U64, b: U64): U64 {
2053
return [a[0] ^ b[0], a[1] ^ b[1], a[2] ^ b[2], a[3] ^ b[3]]
2154
}
@@ -52,6 +85,18 @@ function _shiftLeft(value: U64, bits: number): U64 {
5285
]
5386
}
5487

88+
/**
89+
* Shifts the bits of a `U64` value to the left. Equivalent to the numeric `<<`
90+
* operator.
91+
*
92+
* @param value - Value to shift.
93+
* @param bits - Number of bits to shift by.
94+
* @param overflow - Whether to allow overflow in the most significant block.
95+
*
96+
* @returns The shifted value.
97+
*
98+
* @category Bitwise
99+
*/
55100
export function shiftLeft(value: U64, bits: number, overflow = false): U64 {
56101
return overflow
57102
? softClampBlocks(_shiftLeft(value, bits))
@@ -90,38 +135,69 @@ function _shiftRight(value: U64, bits: number): U64 {
90135
]
91136
}
92137

138+
/**
139+
* Shifts the bits of a `U64` value to the right. Equivalent to the numeric `>>`
140+
* operator.
141+
*
142+
* @param value - Value to shift.
143+
* @param bits - Number of bits to shift by.
144+
*
145+
* @returns The shifted value.
146+
*
147+
* @category Bitwise
148+
*/
93149
export function shiftRight(value: U64, bits: number): U64 {
94150
return clampBlocks(_shiftRight(value, bits))
95151
}
96152

97-
export function rotateLeft(u64: U64, bits: number): U64 {
153+
/**
154+
* Rotates the bits of a `U64` value to the left.
155+
*
156+
* @param value - Value to rotate.
157+
* @param bits - Number of bits to rotate by.
158+
*
159+
* @returns The rotated value.
160+
*
161+
* @category Bitwise
162+
*/
163+
export function rotateLeft(value: U64, bits: number): U64 {
98164
const _bits = bits % 64
99165

100166
const bitsToShift = _bits >= 32 ? _bits - 32 : _bits
101167

102-
const _u64: U64 = _bits >= 32 ? [u64[2], u64[3], u64[0], u64[1]] : u64
168+
const _value: U64 = _bits >= 32 ? [value[2], value[3], value[0], value[1]] : value
103169

104-
if (bitsToShift === 0) return _u64
170+
if (bitsToShift === 0) return _value
105171

106-
const h = (_u64[3] << 16) | _u64[2]
107-
const l = (_u64[1] << 16) | _u64[0]
172+
const h = (_value[3] << 16) | _value[2]
173+
const l = (_value[1] << 16) | _value[0]
108174

109175
const high = (h << bitsToShift) | (l >>> (32 - bitsToShift))
110176
const low = (l << bitsToShift) | (h >>> (32 - bitsToShift))
111177

112178
return [clamp(low), overflow(low), clamp(high), overflow(high)]
113179
}
114180

115-
export function rotateRight(u64: U64, bits: number): U64 {
181+
/**
182+
* Rotates the bits of a `U64` value to the right.
183+
*
184+
* @param value - Value to rotate.
185+
* @param bits - Number of bits to rotate by.
186+
*
187+
* @returns The rotated value.
188+
*
189+
* @category Bitwise
190+
*/
191+
export function rotateRight(value: U64, bits: number): U64 {
116192
const _bits = bits % 64
117193

118194
const bitsToShift = _bits >= 32 ? _bits - 32 : _bits
119-
const _u64: U64 = _bits >= 32 ? [u64[2], u64[3], u64[0], u64[1]] : u64
195+
const _value: U64 = _bits >= 32 ? [value[2], value[3], value[0], value[1]] : value
120196

121-
if (bitsToShift === 0) return _u64
197+
if (bitsToShift === 0) return _value
122198

123-
const h = (_u64[3] << 16) | _u64[2]
124-
const l = (_u64[1] << 16) | _u64[0]
199+
const h = (_value[3] << 16) | _value[2]
200+
const l = (_value[1] << 16) | _value[0]
125201

126202
const high = (h >>> bitsToShift) | (l << (32 - bitsToShift))
127203
const low = (l >>> bitsToShift) | (h << (32 - bitsToShift))

0 commit comments

Comments
 (0)