Skip to content

Commit 6e1e6e8

Browse files
authored
Added HammingCode.js
Alternative with parity bit check.
1 parent 1de5ab7 commit 6e1e6e8

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

Bit-Manipulation/HammingCode.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* github author: chrdek
3+
* license: GPL-3.0 or later
4+
*
5+
* @param {number} b
6+
*
7+
* The following code generates the hamming code for
8+
* 32-bit integer values (incl. parity bit check)
9+
*
10+
**/
11+
12+
function HammingCode(b) {
13+
var bytes = new Array(65536);
14+
const bitAlloc = (bin) => {
15+
var cnt = 0;
16+
while (bin > 0) {
17+
cnt += bin & 1;
18+
bin >>=1;
19+
}
20+
return cnt;
21+
} //end of bitallocation
22+
23+
for (var k=0; k < 65536; k++) bytes[k] = bitAlloc(k);
24+
25+
//perform leftmost shifting for integer value
26+
return bytes[bin & 0xFFFF] + bytes[b >> 16];
27+
}
28+
29+
export { HammingCode }

0 commit comments

Comments
 (0)