Skip to content

Commit c268203

Browse files
committedJun 27, 2018
Add more bit manipulation functions.
1 parent 792f490 commit c268203

File tree

6 files changed

+73
-1
lines changed

6 files changed

+73
-1
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +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
52+
* `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits, multiplication/division by two etc.
5353
* `B` [Factorial](src/algorithms/math/factorial)
5454
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
5555
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)

‎src/algorithms/math/bits/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,37 @@ unsets the bit.
3535
This method is a combination of "Clear Bit" and "Set Bit" methods.
3636

3737
> 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+
#### Divide By Two
57+
58+
This method shifts original number by one bit to the right.
59+
Thus all binary number components (powers of two) are being
60+
divided by two and thus the number itself is being
61+
divided by two without remainder.
62+
63+
```
64+
Before the shift
65+
Number: 0b0101 = 5
66+
Powers of two: 0 + 2^2 + 0 + 2^0
67+
68+
After the shift
69+
Number: 0b0010 = 2
70+
Powers of two: 0 + 0 + 2^1 + 0
71+
```
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+
});
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+
});
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number} number
3+
* @return {number}
4+
*/
5+
export default function divideByTwo(number) {
6+
return number >> 1;
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/**
2+
* @param {number} number
3+
* @return {number}
4+
*/
5+
export default function multiplyByTwo(number) {
6+
return number << 1;
7+
}

0 commit comments

Comments
 (0)
Please sign in to comment.