Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: Hamming Code for 32bit values #1431

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

Open
chrdek wants to merge 20 commits into TheAlgorithms:master
base: master
Choose a base branch
Loading
from chrdek:master
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6e1e6e8
Added HammingCode.js
chrdek Oct 4, 2023
27aa6c0
Tests for HammingCode.js
chrdek Oct 4, 2023
060ee86
Delete Bit-Manipulation/HammingCode.js
chrdek Oct 4, 2023
0c79609
feat: HammingCode for 32bit values
chrdek Oct 4, 2023
039a572
Update HammingCode.test.js (fixes for tests)
chrdek Oct 4, 2023
d3b2f1c
Update HammingCode.test.js
chrdek Oct 4, 2023
002038f
Update HammingCode.test.js
chrdek Oct 4, 2023
3bc23af
Update HammingCode.test.js
chrdek Oct 4, 2023
e081542
Update HammingCode.js
chrdek Oct 4, 2023
cabbc71
Update HammingCode.test.js
chrdek Oct 4, 2023
5258492
Create TriangularNumberCheck.test.js
chrdek Oct 6, 2023
9539ec0
Create TriangularNumberCheck.js
chrdek Oct 6, 2023
934d81d
Update TriangularNumberCheck.js
chrdek Oct 6, 2023
85dc4c6
Update TriangularNumberCheck.test.js
chrdek Oct 6, 2023
a989afb
Delete Maths/TriangularNumberCheck.js
chrdek Oct 9, 2023
5fa4d6e
Delete Maths/test/TriangularNumberCheck.test.js
chrdek Oct 9, 2023
a35ce8d
Create HammingWeight.js
chrdek Oct 9, 2023
7004544
Create HammingWeight.test.js
chrdek Oct 9, 2023
a288dc8
Delete Bit-Manipulation/test/HammingCode.test.js
chrdek Oct 9, 2023
2f0fa72
Delete Bit-Manipulation/HammingCode.js
chrdek Oct 9, 2023
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
29 changes: 29 additions & 0 deletions Bit-Manipulation/HammingCode.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* github author: chrdek
* license: GPL-3.0 or later
*
* @param {number} b
*
* The following code generates the hamming code for
* 32-bit integer values (incl. parity bit check)
*
**/

function HammingCode(b) {
var bytes = new Array(65536);
const bitAlloc = (bin) => {
var cnt = 0;
while (bin > 0) {
cnt += bin & 1;
bin >>=1;
}
return cnt;
} //end of bitallocation

for (var k=0; k < 65536; k++) bytes[k] = bitAlloc(k);

//perform leftmost shifting for integer value
return bytes[b & 0xFFFF] + bytes[b >> 16];
}

export { HammingCode }
41 changes: 41 additions & 0 deletions Bit-Manipulation/test/HammingCode.test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { HammingCode } from '../HammingCode'

test('checks HammingCode of 1110101 hex converted is 5', () => {
let code = HammingCode(parseInt("1110101",16))
expect(code).toBe(5)
})

test('checks HammingCode of 10043091 hex converted is 7', () => {
let code = HammingCode(parseInt("10043091",16))
expect(code).toBe(7)
})

test('checks HammingCode of 889193 hex converted is 9', () => {
let code = HammingCode(parseInt("889193",16))
expect(code).toBe(9)
})

test('checks HammingCode of 1110101 hex converted is 5', () => {
let code = HammingCode(parseInt("1101110",2))
expect(code).toBe(5)
})

test('checks HammingCode of 1101110 is 5', () => {
let code = HammingCode(1101110)
expect(code).toBe(5)
})

test('checks HammingCode of 0x10043091 is 7', () => {
let code = HammingCode(0x10043091)
expect(code).toBe(7)
})

test('checks HammingCode of 0x8891930311 is NaN (above range)', () => {
let code = HammingCode(0x8891930311)
expect(code).toBeNaN()
})

test('checks HammingCode of the Number 100 is 3', () => {
let code = HammingCode((Number(100))
expect(code).toBe(3)
})

AltStyle によって変換されたページ (->オリジナル) /