File tree 4 files changed +39
-25
lines changed
4 files changed +39
-25
lines changed Original file line number Diff line number Diff line change @@ -91,20 +91,6 @@ inverting all of the bits of the number and adding 1 to it.
91
91
92
92
> See ` switchSign ` function for further details.
93
93
94
- #### Count Bits to Flip One Number to Another
95
-
96
- This methods outputs the number of bits required to convert a number to another. This
97
- makes use of property that when numbers are XORed the result will be number of different
98
- bits and ` countSetBits ` .
99
-
100
- ``
101
- Number A : 5 = (0101)_ 2
102
- Number B : 1 = (0001)_ 2
103
- Count Bits to be Flipped: 1
104
- ``
105
-
106
- > See ` countBitsToflipAToB ` function for further details.
107
-
108
94
#### Multiply Two Numbers
109
95
110
96
This method multiplies two integer numbers using bitwise operators.
@@ -143,6 +129,19 @@ Count of set bits = 2
143
129
144
130
> See ` countSetBits ` function for further details.
145
131
132
+ #### Count Bits to Flip One Number to Another
133
+
134
+ This methods outputs the number of bits required to convert one number to another.
135
+ This makes use of property that when numbers are ` XOR ` -ed the result will be number
136
+ of different bits.
137
+
138
+ ```
139
+ 5 = 0b0101
140
+ 1 = 0b0001
141
+ Count of Bits to be Flipped: 1
142
+ ```
143
+
144
+ > See ` bitsDiff ` function for further details.
146
145
147
146
## References
148
147
Original file line number Diff line number Diff line change
1
+ import bitsDiff from '../bitsDiff' ;
2
+
3
+ describe ( 'bitsDiff' , ( ) => {
4
+ it ( 'should calculate bits difference between two numbers' , ( ) => {
5
+ expect ( bitsDiff ( 0 , 0 ) ) . toBe ( 0 ) ;
6
+ expect ( bitsDiff ( 1 , 1 ) ) . toBe ( 0 ) ;
7
+ expect ( bitsDiff ( 124 , 124 ) ) . toBe ( 0 ) ;
8
+ expect ( bitsDiff ( 0 , 1 ) ) . toBe ( 1 ) ;
9
+ expect ( bitsDiff ( 1 , 0 ) ) . toBe ( 1 ) ;
10
+ expect ( bitsDiff ( 1 , 2 ) ) . toBe ( 2 ) ;
11
+ expect ( bitsDiff ( 1 , 3 ) ) . toBe ( 1 ) ;
12
+ } ) ;
13
+ } ) ;
Original file line number Diff line number Diff line change
1
+ import countSetBits from './countSetBits' ;
2
+
3
+ /**
4
+ * Counts the number of bits that need to be change in order
5
+ * to convert numberA to numberB.
6
+ *
7
+ * @param {number } numberA
8
+ * @param {number } numberB
9
+ * @return {number }
10
+ */
11
+ export default function bitsDiff ( numberA , numberB ) {
12
+ return countSetBits ( numberA ^ numberB ) ;
13
+ }
Load Diff This file was deleted.
You can’t perform that action at this time.
0 commit comments