mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-26 07:01:18 +08:00
Add countBitsToflipAToB (#154)
This commit is contained in:
parent
6c9641aa3d
commit
2361e6fc44
@ -91,6 +91,20 @@ inverting all of the bits of the number and adding 1 to it.
|
|||||||
|
|
||||||
> See `switchSign` function for further details.
|
> See `switchSign` function for further details.
|
||||||
|
|
||||||
|
#### Count Bits to Flip One Number to Another
|
||||||
|
|
||||||
|
This methods outputs the number of bits required to convert a number to another. This
|
||||||
|
makes use of property that when numbers are XORed the result will be number of different
|
||||||
|
bits and `countSetBits`.
|
||||||
|
|
||||||
|
``
|
||||||
|
Number A : 5 = (0101)_2
|
||||||
|
Number B : 1 = (0001)_2
|
||||||
|
Count Bits to be Flipped: 1
|
||||||
|
``
|
||||||
|
|
||||||
|
> See `countBitsToflipAToB` function for further details.
|
||||||
|
|
||||||
#### Multiply Two Numbers
|
#### Multiply Two Numbers
|
||||||
|
|
||||||
This method multiplies two integer numbers using bitwise operators.
|
This method multiplies two integer numbers using bitwise operators.
|
||||||
@ -129,6 +143,7 @@ Count of set bits = 2
|
|||||||
|
|
||||||
> See `countSetBits` function for further details.
|
> See `countSetBits` function for further details.
|
||||||
|
|
||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
- [Bit Manipulation on YouTube](https://www.youtube.com/watch?v=NLKQEOgBAnw&t=0s&index=28&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
|
- [Bit Manipulation on YouTube](https://www.youtube.com/watch?v=NLKQEOgBAnw&t=0s&index=28&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
|
||||||
|
11
src/algorithms/math/bits/countBitsToflipAToB.js
Normal file
11
src/algorithms/math/bits/countBitsToflipAToB.js
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
import countSetBits from 'countSetBits';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {number} number
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
export default function countBitsToflipAToB(numberA, numberB) {
|
||||||
|
|
||||||
|
return countSetBits(numberA ^ numberB);
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user