-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
6e1e6e8
Added HammingCode.js
chrdek 27aa6c0
Tests for HammingCode.js
chrdek 060ee86
Delete Bit-Manipulation/HammingCode.js
chrdek 0c79609
feat: HammingCode for 32bit values
chrdek 039a572
Update HammingCode.test.js (fixes for tests)
chrdek d3b2f1c
Update HammingCode.test.js
chrdek 002038f
Update HammingCode.test.js
chrdek 3bc23af
Update HammingCode.test.js
chrdek e081542
Update HammingCode.js
chrdek cabbc71
Update HammingCode.test.js
chrdek 5258492
Create TriangularNumberCheck.test.js
chrdek 9539ec0
Create TriangularNumberCheck.js
chrdek 934d81d
Update TriangularNumberCheck.js
chrdek 85dc4c6
Update TriangularNumberCheck.test.js
chrdek a989afb
Delete Maths/TriangularNumberCheck.js
chrdek 5fa4d6e
Delete Maths/test/TriangularNumberCheck.test.js
chrdek a35ce8d
Create HammingWeight.js
chrdek 7004544
Create HammingWeight.test.js
chrdek a288dc8
Delete Bit-Manipulation/test/HammingCode.test.js
chrdek 2f0fa72
Delete Bit-Manipulation/HammingCode.js
chrdek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.