File tree 6 files changed +73
-1
lines changed
6 files changed +73
-1
lines changed Original file line number Diff line number Diff line change @@ -49,7 +49,7 @@ a set of rules that precisely define a sequence of operations.
49
49
### Algorithms by Topic
50
50
51
51
* ** 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.
53
53
* ` B ` [ Factorial] ( src/algorithms/math/factorial )
54
54
* ` B ` [ Fibonacci Number] ( src/algorithms/math/fibonacci )
55
55
* ` B ` [ Primality Test] ( src/algorithms/math/primality-test ) (trial division method)
Original file line number Diff line number Diff line change @@ -35,3 +35,37 @@ unsets the bit.
35
35
This method is a combination of "Clear Bit" and "Set Bit" methods.
36
36
37
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
+ #### 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 number Diff line number Diff line change
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 number Diff line number Diff line change
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
+ } ) ;
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } number
3
+ * @return {number }
4
+ */
5
+ export default function divideByTwo ( number ) {
6
+ return number >> 1 ;
7
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * @param {number } number
3
+ * @return {number }
4
+ */
5
+ export default function multiplyByTwo ( number ) {
6
+ return number << 1 ;
7
+ }
You can’t perform that action at this time.
0 commit comments