Skip to content

Add multiplyBy7 #156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/algorithms/math/bits/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ inverting all of the bits of the number and adding 1 to it.

> See `switchSign` function for further details.

#### Multiply By 7

This method multiplies a number by 7 using bitwise operator. It uses the property that
`8*n - n` would be `7n` multiplied by the number.

```
Muliplier : 5
Multiplied by 7 : 35
```

> See `multiplyBy7` function for further details.

## References

- [Bit Manipulation on YouTube](https://www.youtube.com/watch?v=NLKQEOgBAnw&t=0s&index=28&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
Expand Down
8 changes: 8 additions & 0 deletions src/algorithms/math/bits/__test__/multiplyBy7.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import multiplyBy7 from '../multiplyBy7';

describe('multiplyBy7', () => {
it('Should return result multiplied by 7', () => {
expect(multiplyBy7(1)).toBe(7);
expect(multiplyBy7(2)).toBe(14);
});
});
7 changes: 7 additions & 0 deletions src/algorithms/math/bits/multiplyBy7.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
/**
* @param {number} number
* @return {number}
*/
export default function multiplyBy7(number) {
return ((number << 3) - number);
}