Add bit manipulation section.

This commit is contained in:
Oleksii Trekhleb
2018-06-27 17:19:20 +03:00
parent 36e0bfeb32
commit 792f4906df
10 changed files with 152 additions and 0 deletions

View File

@ -49,6 +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
* `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)

View File

@ -0,0 +1,37 @@
# Bit Manipulation
#### Get Bit
This method shifts `1` over by `bitPosition` bits, creating a
value that looks like `00100`. Then we perform `AND` operation
that clears all bits from the original number except the
`bitPosition` one. Then we compare the result with zero. If
result is zero that would mean that original number has `0` at
position `bitPosition`.
> See `getBit` function for further details.
#### Set Bit
This method shifts `1` over by `bitPosition` bits, creating a
value that looks like `00100`. Then we perform `OR` operation
that sets specific bit into `1` but it does not affect on
other bits of the number.
> See `setBit` function for further details.
#### Clear Bit
This method shifts `1` over by `bitPosition` bits, creating a
value that looks like `00100`. Than it inverts this mask to get
the number that looks like `11011`. Then `AND` operation is
being applied to both the number and the mask. That operation
unsets the bit.
> See `clearBit` function for further details.
#### Update Bit
This method is a combination of "Clear Bit" and "Set Bit" methods.
> See `updateBit` function for further details.

View File

@ -0,0 +1,15 @@
import clearBit from '../clearBit';
describe('clearBit', () => {
it('should clear bit at specific position', () => {
// 1 = 0b0001
expect(clearBit(1, 0)).toBe(0);
expect(clearBit(1, 1)).toBe(1);
expect(clearBit(1, 2)).toBe(1);
// 10 = 0b1010
expect(clearBit(10, 0)).toBe(10);
expect(clearBit(10, 1)).toBe(8);
expect(clearBit(10, 3)).toBe(2);
});
});

View File

@ -0,0 +1,23 @@
import getBit from '../getBit';
describe('getBit', () => {
it('should get bit at specific position', () => {
// 1 = 0b0001
expect(getBit(1, 0)).toBe(1);
expect(getBit(1, 1)).toBe(0);
// 2 = 0b0010
expect(getBit(2, 0)).toBe(0);
expect(getBit(2, 1)).toBe(1);
// 3 = 0b0011
expect(getBit(3, 0)).toBe(1);
expect(getBit(3, 1)).toBe(1);
// 10 = 0b1010
expect(getBit(10, 0)).toBe(0);
expect(getBit(10, 1)).toBe(1);
expect(getBit(10, 2)).toBe(0);
expect(getBit(10, 3)).toBe(1);
});
});

View File

@ -0,0 +1,15 @@
import setBit from '../setBit';
describe('setBit', () => {
it('should set bit at specific position', () => {
// 1 = 0b0001
expect(setBit(1, 0)).toBe(1);
expect(setBit(1, 1)).toBe(3);
expect(setBit(1, 2)).toBe(5);
// 10 = 0b1010
expect(setBit(10, 0)).toBe(11);
expect(setBit(10, 1)).toBe(10);
expect(setBit(10, 2)).toBe(14);
});
});

View File

@ -0,0 +1,19 @@
import updateBit from '../updateBit';
describe('updateBit', () => {
it('should update bit at specific position', () => {
// 1 = 0b0001
expect(updateBit(1, 0, 1)).toBe(1);
expect(updateBit(1, 0, 0)).toBe(0);
expect(updateBit(1, 1, 1)).toBe(3);
expect(updateBit(1, 2, 1)).toBe(5);
// 10 = 0b1010
expect(updateBit(10, 0, 1)).toBe(11);
expect(updateBit(10, 0, 0)).toBe(10);
expect(updateBit(10, 1, 1)).toBe(10);
expect(updateBit(10, 1, 0)).toBe(8);
expect(updateBit(10, 2, 1)).toBe(14);
expect(updateBit(10, 2, 0)).toBe(10);
});
});

View File

@ -0,0 +1,10 @@
/**
* @param {number} number
* @param {number} bitPosition - zero based.
* @return {number}
*/
export default function clearBit(number, bitPosition) {
const mask = ~(1 << bitPosition);
return number & mask;
}

View File

@ -0,0 +1,8 @@
/**
* @param {number} number
* @param {number} bitPosition - zero based.
* @return {number}
*/
export default function getBit(number, bitPosition) {
return (number & (1 << bitPosition)) === 0 ? 0 : 1;
}

View File

@ -0,0 +1,8 @@
/**
* @param {number} number
* @param {number} bitPosition - zero based.
* @return {number}
*/
export default function setBit(number, bitPosition) {
return number | (1 << bitPosition);
}

View File

@ -0,0 +1,16 @@
/**
* @param {number} number
* @param {number} bitPosition - zero based.
* @param {number} bitValue - 0 or 1.
* @return {number}
*/
export default function updateBit(number, bitPosition, bitValue) {
// Normalized bit value.
const bitValueNormalized = bitValue ? 1 : 0;
// Init clear mask.
const clearMask = ~(1 << bitPosition);
// Cleat bit value and then set it up to required value.
return (number & clearMask) | (bitValueNormalized << bitPosition);
}