Skip to content
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

Add bit partitioning #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
14 changes: 13 additions & 1 deletion src/algorithms/math/bits/README.md
Original file line number Diff line number Diff line change
@@ -53,7 +53,7 @@ isEven: true
#### isPositive

This method determines if the number is positive. It is based on the fact that all positive
This method determines if the number is positive. It is based on the fact that all positive
numbers have their leftmost bit to be set to `0`. However, if the number provided is zero
or negative zero, it should still return `false`.

@@ -226,6 +226,18 @@ Number: 9 = (10 - 1) = 0b01001

> See [isPowerOfTwo.js](isPowerOfTwo.js) for further details.
#### Bit Partitioning

This method rewrites a number in a base which is a power of 2.
For example:

```
509 = 7 * ((2 ** 3) ** 2) + 7 * ((2 ** 3) ** 1) + 5 * ((2 ** 3) ** 0)
```

Therefore 509 (in base 10) == 775 (in base (2 ** 3))

> See [bitPartition.js](bitPartition.js) for further details.
## References

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

describe('bitPartition', () => {
it('should calculate number in a base which is a power of 2', () => {
const bits = 3;
const base = 2 ** bits;

expect(bitPartition(0, bits)).toEqual([0]);
expect(bitPartition(base ** 0, bits)).toEqual([1]);
expect(bitPartition((base ** 3) - 1, bits)).toEqual([7, 7, 7]);
expect(bitPartition(base ** 3, bits)).toEqual([1, 0, 0, 0]);
});
});
32 changes: 32 additions & 0 deletions src/algorithms/math/bits/bitPartition.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Return the number represented in a base which is a power of 2
* For example given x >= 0, it can be rewritten in base 2 ** k as:
* x = xn * ((2 ** k) ** n) + ... + x0 * ((2 ** k) ** 0)
* where
* 0 <= xn, ... , x0 <= (2 ** k) - 1
* x < (2 ** k) ** (n + 1)
*
* @param {number} number
* @param {number} number
* @return {number[]}
*/

export default function bitPartition(num, bits) {
const mask = (1 << bits) - 1;

// find shift
// where
// shift = n * bits
// num < (2 ** bits) ** (n + 1)
let n = 0;
while (num >= (2 ** bits) ** (n + 1)) {
n += 1;
}

const xs = [];
for (let shift = bits * n; shift >= 0; shift -= bits) {
xs.push((num >>> shift) & mask);
}

return xs;
}