mirror of
https://github.com/myfatemi04/wheelshare-frontend.git
synced 2025-04-21 03:10:17 -04:00
24 lines
494 B
TypeScript
24 lines
494 B
TypeScript
export function setBit(n: number, idx: number, active: boolean) {
|
|
if (idx < 1 || idx > 7 || !isFinite(idx)) {
|
|
throw new Error('invalid idx. idx must be from 1 - 7.');
|
|
}
|
|
|
|
const mask = 0b1000_0000 >> idx;
|
|
|
|
if (active) {
|
|
return n | mask;
|
|
} else {
|
|
return n & ~mask;
|
|
}
|
|
}
|
|
|
|
export function toggleBit(n: number, idx: number) {
|
|
if (idx < 1 || idx > 7 || !isFinite(idx)) {
|
|
throw new Error('invalid idx. idx must be from 1 - 7.');
|
|
}
|
|
|
|
const mask = 0b1000_0000 >> idx;
|
|
|
|
return n ^ mask;
|
|
}
|