Skip to content

Instantly share code, notes, and snippets.

@rednafi
Last active June 7, 2022 22:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rednafi/d8da3f48ba0d7e9afb13498e868bccba to your computer and use it in GitHub Desktop.
Save rednafi/d8da3f48ba0d7e9afb13498e868bccba to your computer and use it in GitHub Desktop.
A simple two bit flip flop adder in TypeScript
type Bit = 0 | 1;
type Zero = Extract<Bit, 0>;
type One = Exclude<Bit, Zero>;
type Flip<T extends Bit> = T extends Zero ? One : Zero;
type Add<T extends Bit, U extends Bit> = T extends Zero
? U extends Zero
? Zero
: One
: One;
type NotAdd<T extends Bit, U extends Bit> = Flip<Add<T, U>>;
type Result = NotAdd<One, Zero>; // 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment