Skip to content

Commit 46658d3

Browse files
committed
Merged fork master with upstream
2 parents c70f380 + b41cffe commit 46658d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+1346
-580
lines changed

README.md

Lines changed: 114 additions & 110 deletions
Large diffs are not rendered by default.

README.zh-CN.md

Lines changed: 90 additions & 90 deletions
Large diffs are not rendered by default.

README.zh-TW.md

Lines changed: 90 additions & 90 deletions
Large diffs are not rendered by default.

src/algorithms/math/bits/README.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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.
38+
39+
#### Multiply By Two
40+
41+
This method shifts original number by one bit to the left.
42+
Thus all binary number components (powers of two) are being
43+
multiplying by two and thus the number itself is being
44+
multiplied by two.
45+
46+
```
47+
Before the shift
48+
Number: 0b0101 = 5
49+
Powers of two: 0 + 2^2 + 0 + 2^0
50+
51+
After the shift
52+
Number: 0b1010 = 10
53+
Powers of two: 2^3 + 0 + 2^1 + 0
54+
```
55+
56+
> See `multiplyByTwo` function for further details.
57+
58+
#### Divide By Two
59+
60+
This method shifts original number by one bit to the right.
61+
Thus all binary number components (powers of two) are being
62+
divided by two and thus the number itself is being
63+
divided by two without remainder.
64+
65+
```
66+
Before the shift
67+
Number: 0b0101 = 5
68+
Powers of two: 0 + 2^2 + 0 + 2^0
69+
70+
After the shift
71+
Number: 0b0010 = 2
72+
Powers of two: 0 + 0 + 2^1 + 0
73+
```
74+
75+
> See `divideByTwo` function for further details.
76+
77+
#### Switch Sign
78+
79+
This method make positive numbers to be negative and backwards.
80+
To do so it uses "Twos Complement" approach which does it by
81+
inverting all of the bits of the number and adding 1 to it.
82+
83+
```
84+
1101 -3
85+
1110 -2
86+
1111 -1
87+
0000 0
88+
0001 1
89+
0010 2
90+
0011 3
91+
```
92+
93+
> See `switchSign` function for further details.
94+
95+
## References
96+
97+
- [Bit Manipulation on YouTube](https://www.youtube.com/watch?v=NLKQEOgBAnw&t=0s&index=28&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
98+
- [Negative Numbers in binary on YouTube](https://www.youtube.com/watch?v=4qH4unVtJkE&t=0s&index=30&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
99+
- [Bit Hacks on stanford.edu](https://graphics.stanford.edu/~seander/bithacks.html)
Lines changed: 15 additions & 0 deletions
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+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import divideByTwo from '../divideByTwo';
2+
3+
describe('divideByTwo', () => {
4+
it('should divide numbers by two using bitwise operations', () => {
5+
expect(divideByTwo(0)).toBe(0);
6+
expect(divideByTwo(1)).toBe(0);
7+
expect(divideByTwo(3)).toBe(1);
8+
expect(divideByTwo(10)).toBe(5);
9+
expect(divideByTwo(17)).toBe(8);
10+
expect(divideByTwo(125)).toBe(62);
11+
});
12+
});
Lines changed: 23 additions & 0 deletions
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+
});
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import multiplyByTwo from '../multiplyByTwo';
2+
3+
describe('multiplyByTwo', () => {
4+
it('should multiply numbers by two using bitwise operations', () => {
5+
expect(multiplyByTwo(0)).toBe(0);
6+
expect(multiplyByTwo(1)).toBe(2);
7+
expect(multiplyByTwo(3)).toBe(6);
8+
expect(multiplyByTwo(10)).toBe(20);
9+
expect(multiplyByTwo(17)).toBe(34);
10+
expect(multiplyByTwo(125)).toBe(250);
11+
});
12+
});
Lines changed: 15 additions & 0 deletions
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+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import switchSign from '../switchSign';
2+
3+
describe('switchSign', () => {
4+
it('should switch the sign of the number using twos complement approach', () => {
5+
expect(switchSign(0)).toBe(0);
6+
expect(switchSign(1)).toBe(-1);
7+
expect(switchSign(-1)).toBe(1);
8+
expect(switchSign(32)).toBe(-32);
9+
expect(switchSign(-32)).toBe(32);
10+
expect(switchSign(23)).toBe(-23);
11+
expect(switchSign(-23)).toBe(23);
12+
});
13+
});

0 commit comments

Comments
 (0)