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 14 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
34 changes: 34 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,34 @@
/**
* github author: chrdek
* license: GPL-3.0 or later
*
* @param {number} b = integer or, binary representation of it
*
* The following code generates the hamming code for a binary sequence
* of 32-bit integer values (incl. parity bit check)
*
* Returns the overall of bit count population for values up to 65535, inclusive
*
**/

function HammingCode(b) {
//preallocate total number of integers to count the bits population from.
let bytes = new Array(65536);
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 { HammingCode }
14 changes: 14 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,14 @@
import { HammingCode } from '../HammingCode'

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 hamming code conversion from $inputVal', ({inputVal, expectedVal}) => {
test(`returns bit count = ${expectedVal}`, () => {
expect(HammingCode(inputVal)).toBe(expectedVal)
})
})
39 changes: 39 additions & 0 deletions Maths/TriangularNumberCheck.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* github author: chrdek
* license: GPL-3.0 or later
*
* @param {number} n = number to be determined whether is a triangular or not
*
* The function below is a mathematical algorithm to check if a given number n is a triangular number.
* A triangular number is one that can be represented
* in the form of the sum of consecutive positive integers starting from 1.
*
* Returns -1 for error input or negative numerical input, 0 for non-triangular
* and an integer value if the number is triangular.
*
* The variable discriminant is calculated as the square root of (1 + 8 * n) and then its square root is taken again.
* The expression Math.sqrt(1 + 8 * n) is the discriminant of the quadratic equation that represents triangular numbers.
*
**/

function TriangularNumberCheck(n) {
// Ensure the input n is a non-negative integer, retrurn -1 to indicate error.
if (!Number.isInteger(n) || n < 0) {
return -1;
}

// Calculate the discriminant of the quadratic equation
const discriminant = Math.sqrt(1 + 8 * n);

// Check if the discriminant is an integer
if (discriminant % 1 === 0) {

// Return half of the integer value of the discriminant
return Math.floor(discriminant / 2);
} else {
// If not a triangular number, return 0
return 0;
}
}

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

describe.each([
{ inputVal:55, expectedVal:10 },
{ inputVal:45, expectedVal:9 },
{ inputVal:190, expectedVal:19 },
{ inputVal:66, expectedVal:11 },
{ inputVal:666, expectedVal:36 },
{ inputVal:6, expectedVal:3 },
{ inputVal:10, expectedVal:4 },
{ inputVal:1, expectedVal:1 },
])('If the number of $inputVal is triangular', ({inputVal, expectedVal}) => {
test(`should return ${expectedVal}`, () => {
expect(TriangularNumberCheck(inputVal)).toBe(expectedVal)
})
})

describe.each([
{ inputVal:-1, expectedVal:-1 },
{ inputVal:-10, expectedVal:-1 },
{ inputVal:-9, expectedVal:-1 },
{ inputVal:"foo", expectedVal:-1 },
{ inputVal:"fizz", expectedVal:-1 },
])('If the number of $inputVal is completely wrong or negative', ({inputVal, expectedVal}) => {
test(`should return ${expectedVal}`, () => {
expect(TriangularNumberCheck(inputVal)).not.toBeLessThan(-1)
})
})

describe.each([
{ inputVal:4183059834009, expectedVal:0 },
{ inputVal:69, expectedVal:0 },
{ inputVal:0, expectedVal:0 },
{ inputVal:23, expectedVal:0 }
])('If the number of $inputVal is NOT triangular', ({inputVal, expectedVal}) => {
test(`should return ${expectedVal}`, () => {
expect(TriangularNumberCheck(inputVal)).toBe(0)
})
})

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