mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-06 17:44:08 +08:00
Add bit manipulation section.
This commit is contained in:
@ -49,6 +49,7 @@ a set of rules that precisely define a sequence of operations.
|
||||
### Algorithms by Topic
|
||||
|
||||
* **Math**
|
||||
* `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits
|
||||
* `B` [Factorial](src/algorithms/math/factorial)
|
||||
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
|
||||
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
|
||||
|
37
src/algorithms/math/bits/README.md
Normal file
37
src/algorithms/math/bits/README.md
Normal file
@ -0,0 +1,37 @@
|
||||
# Bit Manipulation
|
||||
|
||||
#### Get Bit
|
||||
|
||||
This method shifts `1` over by `bitPosition` bits, creating a
|
||||
value that looks like `00100`. Then we perform `AND` operation
|
||||
that clears all bits from the original number except the
|
||||
`bitPosition` one. Then we compare the result with zero. If
|
||||
result is zero that would mean that original number has `0` at
|
||||
position `bitPosition`.
|
||||
|
||||
> See `getBit` function for further details.
|
||||
|
||||
#### Set Bit
|
||||
|
||||
This method shifts `1` over by `bitPosition` bits, creating a
|
||||
value that looks like `00100`. Then we perform `OR` operation
|
||||
that sets specific bit into `1` but it does not affect on
|
||||
other bits of the number.
|
||||
|
||||
> See `setBit` function for further details.
|
||||
|
||||
#### Clear Bit
|
||||
|
||||
This method shifts `1` over by `bitPosition` bits, creating a
|
||||
value that looks like `00100`. Than it inverts this mask to get
|
||||
the number that looks like `11011`. Then `AND` operation is
|
||||
being applied to both the number and the mask. That operation
|
||||
unsets the bit.
|
||||
|
||||
> See `clearBit` function for further details.
|
||||
|
||||
#### Update Bit
|
||||
|
||||
This method is a combination of "Clear Bit" and "Set Bit" methods.
|
||||
|
||||
> See `updateBit` function for further details.
|
15
src/algorithms/math/bits/__test__/clearBit.test.js
Normal file
15
src/algorithms/math/bits/__test__/clearBit.test.js
Normal file
@ -0,0 +1,15 @@
|
||||
import clearBit from '../clearBit';
|
||||
|
||||
describe('clearBit', () => {
|
||||
it('should clear bit at specific position', () => {
|
||||
// 1 = 0b0001
|
||||
expect(clearBit(1, 0)).toBe(0);
|
||||
expect(clearBit(1, 1)).toBe(1);
|
||||
expect(clearBit(1, 2)).toBe(1);
|
||||
|
||||
// 10 = 0b1010
|
||||
expect(clearBit(10, 0)).toBe(10);
|
||||
expect(clearBit(10, 1)).toBe(8);
|
||||
expect(clearBit(10, 3)).toBe(2);
|
||||
});
|
||||
});
|
23
src/algorithms/math/bits/__test__/getBit.test.js
Normal file
23
src/algorithms/math/bits/__test__/getBit.test.js
Normal file
@ -0,0 +1,23 @@
|
||||
import getBit from '../getBit';
|
||||
|
||||
describe('getBit', () => {
|
||||
it('should get bit at specific position', () => {
|
||||
// 1 = 0b0001
|
||||
expect(getBit(1, 0)).toBe(1);
|
||||
expect(getBit(1, 1)).toBe(0);
|
||||
|
||||
// 2 = 0b0010
|
||||
expect(getBit(2, 0)).toBe(0);
|
||||
expect(getBit(2, 1)).toBe(1);
|
||||
|
||||
// 3 = 0b0011
|
||||
expect(getBit(3, 0)).toBe(1);
|
||||
expect(getBit(3, 1)).toBe(1);
|
||||
|
||||
// 10 = 0b1010
|
||||
expect(getBit(10, 0)).toBe(0);
|
||||
expect(getBit(10, 1)).toBe(1);
|
||||
expect(getBit(10, 2)).toBe(0);
|
||||
expect(getBit(10, 3)).toBe(1);
|
||||
});
|
||||
});
|
15
src/algorithms/math/bits/__test__/setBit.test.js
Normal file
15
src/algorithms/math/bits/__test__/setBit.test.js
Normal file
@ -0,0 +1,15 @@
|
||||
import setBit from '../setBit';
|
||||
|
||||
describe('setBit', () => {
|
||||
it('should set bit at specific position', () => {
|
||||
// 1 = 0b0001
|
||||
expect(setBit(1, 0)).toBe(1);
|
||||
expect(setBit(1, 1)).toBe(3);
|
||||
expect(setBit(1, 2)).toBe(5);
|
||||
|
||||
// 10 = 0b1010
|
||||
expect(setBit(10, 0)).toBe(11);
|
||||
expect(setBit(10, 1)).toBe(10);
|
||||
expect(setBit(10, 2)).toBe(14);
|
||||
});
|
||||
});
|
19
src/algorithms/math/bits/__test__/updateBit.test.js
Normal file
19
src/algorithms/math/bits/__test__/updateBit.test.js
Normal file
@ -0,0 +1,19 @@
|
||||
import updateBit from '../updateBit';
|
||||
|
||||
describe('updateBit', () => {
|
||||
it('should update bit at specific position', () => {
|
||||
// 1 = 0b0001
|
||||
expect(updateBit(1, 0, 1)).toBe(1);
|
||||
expect(updateBit(1, 0, 0)).toBe(0);
|
||||
expect(updateBit(1, 1, 1)).toBe(3);
|
||||
expect(updateBit(1, 2, 1)).toBe(5);
|
||||
|
||||
// 10 = 0b1010
|
||||
expect(updateBit(10, 0, 1)).toBe(11);
|
||||
expect(updateBit(10, 0, 0)).toBe(10);
|
||||
expect(updateBit(10, 1, 1)).toBe(10);
|
||||
expect(updateBit(10, 1, 0)).toBe(8);
|
||||
expect(updateBit(10, 2, 1)).toBe(14);
|
||||
expect(updateBit(10, 2, 0)).toBe(10);
|
||||
});
|
||||
});
|
10
src/algorithms/math/bits/clearBit.js
Normal file
10
src/algorithms/math/bits/clearBit.js
Normal file
@ -0,0 +1,10 @@
|
||||
/**
|
||||
* @param {number} number
|
||||
* @param {number} bitPosition - zero based.
|
||||
* @return {number}
|
||||
*/
|
||||
export default function clearBit(number, bitPosition) {
|
||||
const mask = ~(1 << bitPosition);
|
||||
|
||||
return number & mask;
|
||||
}
|
8
src/algorithms/math/bits/getBit.js
Normal file
8
src/algorithms/math/bits/getBit.js
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* @param {number} number
|
||||
* @param {number} bitPosition - zero based.
|
||||
* @return {number}
|
||||
*/
|
||||
export default function getBit(number, bitPosition) {
|
||||
return (number & (1 << bitPosition)) === 0 ? 0 : 1;
|
||||
}
|
8
src/algorithms/math/bits/setBit.js
Normal file
8
src/algorithms/math/bits/setBit.js
Normal file
@ -0,0 +1,8 @@
|
||||
/**
|
||||
* @param {number} number
|
||||
* @param {number} bitPosition - zero based.
|
||||
* @return {number}
|
||||
*/
|
||||
export default function setBit(number, bitPosition) {
|
||||
return number | (1 << bitPosition);
|
||||
}
|
16
src/algorithms/math/bits/updateBit.js
Normal file
16
src/algorithms/math/bits/updateBit.js
Normal file
@ -0,0 +1,16 @@
|
||||
/**
|
||||
* @param {number} number
|
||||
* @param {number} bitPosition - zero based.
|
||||
* @param {number} bitValue - 0 or 1.
|
||||
* @return {number}
|
||||
*/
|
||||
export default function updateBit(number, bitPosition, bitValue) {
|
||||
// Normalized bit value.
|
||||
const bitValueNormalized = bitValue ? 1 : 0;
|
||||
|
||||
// Init clear mask.
|
||||
const clearMask = ~(1 << bitPosition);
|
||||
|
||||
// Cleat bit value and then set it up to required value.
|
||||
return (number & clearMask) | (bitValueNormalized << bitPosition);
|
||||
}
|
Reference in New Issue
Block a user