Skip to content

Commit e081542

Browse files
authored
Update HammingCode.js
Changes according to specs. - more understandable variable names/ comments etc. - usage of let/const Note: code refers to bit population count of a binary sequence
1 parent 3bc23af commit e081542

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

Bit-Manipulation/HammingCode.js

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,33 @@
22
* github author: chrdek
33
* license: GPL-3.0 or later
44
*
5-
* @param {number} b
5+
* @param {number} b = integer or, binary representation of it
66
*
7-
* The following code generates the hamming code for
8-
* 32-bit integer values (incl. parity bit check)
7+
* The following code generates the hamming code for a binary sequence
8+
* of 32-bit integer values (incl. parity bit check)
9+
*
10+
* Returns the overall of bit count population for values up to 65535, inclusive
911
*
1012
**/
1113

1214
function HammingCode(b) {
13-
var bytes = new Array(65536);
15+
//preallocate total number of integers to count the bits population from.
16+
let bytes = new Array(65536);
1417
const bitAlloc = (bin) => {
15-
var cnt = 0;
18+
let counter = 0;
1619
while (bin > 0) {
17-
cnt += bin & 1;
20+
counter += bin & 1;
1821
bin >>=1;
1922
}
20-
return cnt;
21-
} //end of bitallocation
23+
return counter;
24+
}
2225

23-
for (var k=0; k < 65536; k++) bytes[k] = bitAlloc(k);
26+
//count all 1-bits from entire bit set
27+
for (let k=0; k < 65536; k++)
28+
bytes[k] = bitAlloc(k);
2429

25-
//perform leftmost shifting for integer value
30+
//perform bit shifting for integer values for bit-populated result
2631
return bytes[b & 0xFFFF] + bytes[b >> 16];
2732
}
2833

29-
export { HammingCode }
34+
export { HammingCode }

0 commit comments

Comments
 (0)