Skip to content

Commit

Permalink
feat(binary): add binary/one-hot conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Feb 26, 2021
1 parent 4f242c8 commit eeb6396
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/binary/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export * from "./float";
export * from "./gray";
export * from "./logic";
export * from "./mask";
export * from "./one-hot";
export * from "./pow";
export * from "./rotate";
export * from "./splat";
Expand Down
22 changes: 22 additions & 0 deletions packages/binary/src/one-hot.ts
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);

0 comments on commit eeb6396

Please sign in to comment.