diff --git a/Bit-Manipulation/HammingWeight.js b/Bit-Manipulation/HammingWeight.js new file mode 100644 index 0000000000..c55ee7a0a3 --- /dev/null +++ b/Bit-Manipulation/HammingWeight.js @@ -0,0 +1,40 @@ +/** + * github author: chrdek + * license: GPL-3.0 or later + * + * @param {number} b = integer or, binary representation of it + * + * The following code generates the "hamming weight" for a binary sequence + * of 32-bit integer values, producing the relevant integer code for it. + * + * Returns the overall of bit count population for values up to 65535, inclusive. + * + * This algorithm utilizes minimum amount of byte space reads and can be very easily + * extended to its 64-bit counterpart. + * + **/ + +function HammingWeight(b) { + //preallocate total number of bytes to count the bits population from + let bytes = new Array(65536); + + //count the bit allocation of the binary sequence by shift in place of the resulting bits + //can be used with xor as well. + const bitAlloc = (bin) => { + let counter = 0; + while (bin > 0) { + counter += bin & 1; + bin >>=1; + } + return counter; + } + + //count all 1-bits from entire bit set + for (let k=0; k < 65536; k++) + bytes[k] = bitAlloc(k); + + //perform bit shifting for integer values for bit-populated result + return bytes[b & 0xFFFF] + bytes[b >> 16]; +} + +export { HammingWeight } diff --git a/Bit-Manipulation/test/HammingWeight.test.js b/Bit-Manipulation/test/HammingWeight.test.js new file mode 100644 index 0000000000..117cbc084c --- /dev/null +++ b/Bit-Manipulation/test/HammingWeight.test.js @@ -0,0 +1,14 @@ +import { HammingWeight } from '../HammingWeight' + +describe.each([ + { inputVal:Number(117), expectedVal: 5 }, + { inputVal:parseInt(0x9F9,16), expectedVal: 7 }, + { inputVal:parseInt(0x889193,16), expectedVal: 10 }, + { inputVal:10043091, expectedVal: 14}, + { inputVal:parseInt("1101110",2), expectedVal: 5 }, + { inputVal:parseInt(1101110,2), expectedVal: 5 } +])('Resulting code checks from $inputVal', ({inputVal, expectedVal}) => { + test(`returns ${expectedVal}`, () => { + expect(HammingWeight(inputVal)).toBe(expectedVal) + }) +})