mirror of
https://github.com/trekhleb/javascript-algorithms.git
synced 2025-07-11 06:02:32 +08:00
Add more bit manipulation functions.
This commit is contained in:
@ -49,7 +49,7 @@ a set of rules that precisely define a sequence of operations.
|
|||||||
### Algorithms by Topic
|
### Algorithms by Topic
|
||||||
|
|
||||||
* **Math**
|
* **Math**
|
||||||
* `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits, multiplication/division by two etc.
|
* `B` [Bit Manipulation](src/algorithms/math/bits) - set/get/update/clear bits, multiplication/division by two, make negative etc.
|
||||||
* `B` [Factorial](src/algorithms/math/factorial)
|
* `B` [Factorial](src/algorithms/math/factorial)
|
||||||
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
|
* `B` [Fibonacci Number](src/algorithms/math/fibonacci)
|
||||||
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
|
* `B` [Primality Test](src/algorithms/math/primality-test) (trial division method)
|
||||||
|
@ -53,6 +53,8 @@ Number: 0b1010 = 10
|
|||||||
Powers of two: 2^3 + 0 + 2^1 + 0
|
Powers of two: 2^3 + 0 + 2^1 + 0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> See `multiplyByTwo` function for further details.
|
||||||
|
|
||||||
#### Divide By Two
|
#### Divide By Two
|
||||||
|
|
||||||
This method shifts original number by one bit to the right.
|
This method shifts original number by one bit to the right.
|
||||||
@ -69,3 +71,29 @@ After the shift
|
|||||||
Number: 0b0010 = 2
|
Number: 0b0010 = 2
|
||||||
Powers of two: 0 + 0 + 2^1 + 0
|
Powers of two: 0 + 0 + 2^1 + 0
|
||||||
```
|
```
|
||||||
|
|
||||||
|
> See `divideByTwo` function for further details.
|
||||||
|
|
||||||
|
#### Switch Sign
|
||||||
|
|
||||||
|
This method make positive numbers to be negative and backwards.
|
||||||
|
To do so it uses "Twos Complement" approach which does it by
|
||||||
|
inverting all of the bits of the number and adding 1 to it.
|
||||||
|
|
||||||
|
```
|
||||||
|
1101 -3
|
||||||
|
1110 -2
|
||||||
|
1111 -1
|
||||||
|
0000 0
|
||||||
|
0001 1
|
||||||
|
0010 2
|
||||||
|
0011 3
|
||||||
|
```
|
||||||
|
|
||||||
|
> See `switchSign` function for further details.
|
||||||
|
|
||||||
|
## References
|
||||||
|
|
||||||
|
- [Bit Manipulation on YouTube](https://www.youtube.com/watch?v=NLKQEOgBAnw&t=0s&index=28&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
|
||||||
|
- [Negative Numbers in binary on YouTube](https://www.youtube.com/watch?v=4qH4unVtJkE&t=0s&index=30&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
|
||||||
|
- [Bit Hacks on stanford.edu](https://graphics.stanford.edu/~seander/bithacks.html)
|
||||||
|
13
src/algorithms/math/bits/__test__/switchSign.test.js
Normal file
13
src/algorithms/math/bits/__test__/switchSign.test.js
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
import switchSign from '../switchSign';
|
||||||
|
|
||||||
|
describe('switchSign', () => {
|
||||||
|
it('should switch the sign of the number using twos complement approach', () => {
|
||||||
|
expect(switchSign(0)).toBe(0);
|
||||||
|
expect(switchSign(1)).toBe(-1);
|
||||||
|
expect(switchSign(-1)).toBe(1);
|
||||||
|
expect(switchSign(32)).toBe(-32);
|
||||||
|
expect(switchSign(-32)).toBe(32);
|
||||||
|
expect(switchSign(23)).toBe(-23);
|
||||||
|
expect(switchSign(-23)).toBe(23);
|
||||||
|
});
|
||||||
|
});
|
8
src/algorithms/math/bits/switchSign.js
Normal file
8
src/algorithms/math/bits/switchSign.js
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
/**
|
||||||
|
* Switch the sign of the number using "Twos Complement" approach.
|
||||||
|
* @param {number} number
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
export default function switchSign(number) {
|
||||||
|
return ~number + 1;
|
||||||
|
}
|
Reference in New Issue
Block a user