Skip to content

Commit 792f490

Browse files
committed
Add bit manipulation section.
1 parent 36e0bfe commit 792f490

10 files changed

+152
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ a set of rules that precisely define a sequence of operations.
4949
### Algorithms by Topic
5050

5151
* **Math**
52+
* `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits
5253
* `B` [Factorial](src/algorithms/math/factorial)
5354
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
5455
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)

src/algorithms/math/bits/README.md

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Bit Manipulation
2+
3+
#### Get Bit
4+
5+
This method shifts `1` over by `bitPosition` bits, creating a
6+
value that looks like `00100`. Then we perform `AND` operation
7+
that clears all bits from the original number except the
8+
`bitPosition` one. Then we compare the result with zero. If
9+
result is zero that would mean that original number has `0` at
10+
position `bitPosition`.
11+
12+
> See `getBit` function for further details.
13+
14+
#### Set Bit
15+
16+
This method shifts `1` over by `bitPosition` bits, creating a
17+
value that looks like `00100`. Then we perform `OR` operation
18+
that sets specific bit into `1` but it does not affect on
19+
other bits of the number.
20+
21+
> See `setBit` function for further details.
22+
23+
#### Clear Bit
24+
25+
This method shifts `1` over by `bitPosition` bits, creating a
26+
value that looks like `00100`. Than it inverts this mask to get
27+
the number that looks like `11011`. Then `AND` operation is
28+
being applied to both the number and the mask. That operation
29+
unsets the bit.
30+
31+
> See `clearBit` function for further details.
32+
33+
#### Update Bit
34+
35+
This method is a combination of "Clear Bit" and "Set Bit" methods.
36+
37+
> See `updateBit` function for further details.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import clearBit from '../clearBit';
2+
3+
describe('clearBit', () => {
4+
it('should clear bit at specific position', () => {
5+
// 1 = 0b0001
6+
expect(clearBit(1, 0)).toBe(0);
7+
expect(clearBit(1, 1)).toBe(1);
8+
expect(clearBit(1, 2)).toBe(1);
9+
10+
// 10 = 0b1010
11+
expect(clearBit(10, 0)).toBe(10);
12+
expect(clearBit(10, 1)).toBe(8);
13+
expect(clearBit(10, 3)).toBe(2);
14+
});
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import getBit from '../getBit';
2+
3+
describe('getBit', () => {
4+
it('should get bit at specific position', () => {
5+
// 1 = 0b0001
6+
expect(getBit(1, 0)).toBe(1);
7+
expect(getBit(1, 1)).toBe(0);
8+
9+
// 2 = 0b0010
10+
expect(getBit(2, 0)).toBe(0);
11+
expect(getBit(2, 1)).toBe(1);
12+
13+
// 3 = 0b0011
14+
expect(getBit(3, 0)).toBe(1);
15+
expect(getBit(3, 1)).toBe(1);
16+
17+
// 10 = 0b1010
18+
expect(getBit(10, 0)).toBe(0);
19+
expect(getBit(10, 1)).toBe(1);
20+
expect(getBit(10, 2)).toBe(0);
21+
expect(getBit(10, 3)).toBe(1);
22+
});
23+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import setBit from '../setBit';
2+
3+
describe('setBit', () => {
4+
it('should set bit at specific position', () => {
5+
// 1 = 0b0001
6+
expect(setBit(1, 0)).toBe(1);
7+
expect(setBit(1, 1)).toBe(3);
8+
expect(setBit(1, 2)).toBe(5);
9+
10+
// 10 = 0b1010
11+
expect(setBit(10, 0)).toBe(11);
12+
expect(setBit(10, 1)).toBe(10);
13+
expect(setBit(10, 2)).toBe(14);
14+
});
15+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import updateBit from '../updateBit';
2+
3+
describe('updateBit', () => {
4+
it('should update bit at specific position', () => {
5+
// 1 = 0b0001
6+
expect(updateBit(1, 0, 1)).toBe(1);
7+
expect(updateBit(1, 0, 0)).toBe(0);
8+
expect(updateBit(1, 1, 1)).toBe(3);
9+
expect(updateBit(1, 2, 1)).toBe(5);
10+
11+
// 10 = 0b1010
12+
expect(updateBit(10, 0, 1)).toBe(11);
13+
expect(updateBit(10, 0, 0)).toBe(10);
14+
expect(updateBit(10, 1, 1)).toBe(10);
15+
expect(updateBit(10, 1, 0)).toBe(8);
16+
expect(updateBit(10, 2, 1)).toBe(14);
17+
expect(updateBit(10, 2, 0)).toBe(10);
18+
});
19+
});

src/algorithms/math/bits/clearBit.js

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @param {number} number
3+
* @param {number} bitPosition - zero based.
4+
* @return {number}
5+
*/
6+
export default function clearBit(number, bitPosition) {
7+
const mask = ~(1 << bitPosition);
8+
9+
return number & mask;
10+
}

src/algorithms/math/bits/getBit.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param {number} number
3+
* @param {number} bitPosition - zero based.
4+
* @return {number}
5+
*/
6+
export default function getBit(number, bitPosition) {
7+
return (number & (1 << bitPosition)) === 0 ? 0 : 1;
8+
}

src/algorithms/math/bits/setBit.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/**
2+
* @param {number} number
3+
* @param {number} bitPosition - zero based.
4+
* @return {number}
5+
*/
6+
export default function setBit(number, bitPosition) {
7+
return number | (1 << bitPosition);
8+
}

src/algorithms/math/bits/updateBit.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @param {number} number
3+
* @param {number} bitPosition - zero based.
4+
* @param {number} bitValue - 0 or 1.
5+
* @return {number}
6+
*/
7+
export default function updateBit(number, bitPosition, bitValue) {
8+
// Normalized bit value.
9+
const bitValueNormalized = bitValue ? 1 : 0;
10+
11+
// Init clear mask.
12+
const clearMask = ~(1 << bitPosition);
13+
14+
// Cleat bit value and then set it up to required value.
15+
return (number & clearMask) | (bitValueNormalized << bitPosition);
16+
}

0 commit comments

Comments
 (0)