-
-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(binary): add binary/one-hot conversions
- Loading branch information
1 parent
4f242c8
commit eeb6396
Showing
2 changed files
with
23 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,22 @@ | ||
import type { Range0_31 } from "@thi.ng/api"; | ||
import { clz32 } from "./count"; | ||
|
||
/** | ||
* Converts binary `x` to one-hot format. | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/One-hot | ||
* | ||
* @param x | ||
*/ | ||
export const binaryOneHot = (x: Range0_31) => (1 << x) >>> 0; | ||
|
||
/** | ||
* Converts one-hot `x` into binary, i.e. the position of the hot bit. | ||
* | ||
* @remarks | ||
* Reference: https://en.wikipedia.org/wiki/One-hot | ||
* | ||
* @param x | ||
*/ | ||
export const oneHotBinary = (x: number) => 31 - clz32(x); |