├── .gitignore ├── .vscode └── settings.json ├── package.json ├── license ├── readme.md └── src └── ts-8-bit.ts /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | .v -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "typescript.tsdk": "node_modules\\typescript\\lib" 3 | } -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ts-8bit", 3 | "version": "0.8.0", 4 | "description": "Using TypeScript's Type System to do 8-bit Arithmetic", 5 | "main": "index.js", 6 | "keywords": ["typescript", "conditional-types", "8-bit", "math"], 7 | "author": "sinclairzx81", 8 | "license": "MIT", 9 | "devDependencies": { 10 | "typescript": "^4.1.2" 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | ts-8-bit: Using TypeScript's Type System to do 8-bit Arithmetic 2 | 3 | The MIT License (MIT) 4 | 5 | Copyright (c) 2020 Haydn Paterson (sinclair) 6 | 7 | Permission is hereby granted, free of charge, to any person obtaining a copy 8 | of this software and associated documentation files (the "Software"), to deal 9 | in the Software without restriction, including without limitation the rights 10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 11 | copies of the Software, and to permit persons to whom the Software is 12 | furnished to do so, subject to the following conditions: 13 | 14 | The above copyright notice and this permission notice shall be included in 15 | all copies or substantial portions of the Software. 16 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 23 | THE SOFTWARE. -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 |
2 | 3 |

TS-8-bit

4 | 5 |

Using TypeScript's Type System to do 8-bit Arithmetic

6 |
7 | 8 | ```typescript 9 | import { 10 | Byte, Num, Add, Sub, Mul, Div, Mod, Eq, Lt, Gt, Lte, Gte, Lsh, Rsh, And, Xor, Or, Not 11 | } from './ts-8-bit' 12 | 13 | // Arithmetic 14 | type A = Num< Add < Byte<12>, Byte<22> > > // + type A = 34 15 | type B = Num< Sub < Byte<47>, Byte<12> > > // - type B = 35 16 | type C = Num< Mul < Byte<31>, Byte<4> > > // * type C = 124 17 | type D = Num< Div < Byte<16>, Byte<8> > > // / type D = 2 18 | type E = Num< Mod < Byte<19>, Byte<4> > > // % type E = 3 19 | 20 | // Equality 21 | type F = Eq < Byte<10>, Byte<10> > // == type F = true 22 | type G = Lt < Byte<10>, Byte<10> > // < type G = false 23 | type H = Gt < Byte<20>, Byte<10> > // > type H = true 24 | type I = Lte< Byte<10>, Byte<10> > // <= type I = true 25 | type J = Gte< Byte<20>, Byte<10> > // >= type J = true 26 | 27 | // Bitwise 28 | type K = Num< Rsh < Byte<64>, 3 > > // >> type K = 8 29 | type L = Num< Lsh < Byte<23>, 2 > > // << type L = 92 30 | type M = Num< Or < Byte<33>, Byte<7> > > // | type M = 39 31 | type N = Num< And < Byte<12>, Byte<5> > > // & type N = 4 32 | type O = Num< Xor < Byte<22>, Byte<17 > > > // ^ type O = 7 33 | type P = Num< Not < Byte<253> > > // ! type P = 2 34 | ``` 35 | 36 | ## Overview 37 | 38 | ts-8-bit is an experimental library that does 8-bit arithmetic on TypeScript's type system. It does so by applying bitwise operations on tuples of length 8 (1 byte) and leverages TypeScript conditional type inference to give the result. This project was written as a exercise in TypeScript and is offered as is for anyone who may find it of use. 39 | 40 | Requires TypeScript 4.1.2 and above. Example link [here](https://www.typescriptlang.org/play?#code/PTAEFpK6dv4YpcBQJQc6A+rv2d8tM9i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nbg9-bRakkrag4eaEbrZSYPfA4tYI+Q8UurFtZI4w-A4XeKao+lerDddo6A7Re0dvf3bA+lYMbQ9IcI84-JcxdI-7isfw548E4AWdAZYmbo4Vecb3dE+k5JudAzaGYU60udBzZU+Y95edALbad45k5Lc0444Vciew+fdU7PbiajaM5BdmEQ9FbE9Jo7bNrXeldmF7dmcc8KaHaWYs9JdmAtc8786etmFtaC607c4XfWf05JpaZE7tcc66c3bCeM5JoGbk4S+C7KtGeZp9emLRemZ86Toi4VYWdvYpdS+pqfby6y9xq2bM4q5Bf2fi8a4eeHAA8uaa5A869rpuFleedq51uuFg4G5K4AJuHs9Lfy-ZZuA7am8G8GhuF7d+bG+lRuBNZW8q8ZqheS7QbEZuAo9G624ERuBo4ReO-5uuEY-O-25xfVZu968JYDYe4ttlAa544u9lupZa4+9u8k5e7tvdhTYB5Vs5uU87em8qc5o04h4W45uFfu9h9W8ZuREM6R8+7qqZtDpS7h7VCVZx+R5O7Vf1sy8lv250gy5qYx51v1dy9ZfJ5NYq9x91vK51fJ7C-p968deZoI8h46bVBXc54tq9fJt59x-9dNqO-F-48LcPv28jcdtl8mN67jcR5c8J-5uTcl6V7J5V7fY+-F+-YN419lort28vep8W8NG68lf24rbp4t9x9rYJr075-G6bcR7lbt7m51ad+W995N7qsHes8d8D51rHdF4D8t45uneB9D+j7lrO7tbd7W+NGu+T9x43bV5Yf293ee4z7D8W6PZ55Ycz4vei5T8Zpvcj4L4T8fcp5z90TAXaCAA). 41 | 42 | License MIT 43 | 44 | ## Usage 45 | 46 | The following demonstrates basic usage. 47 | 48 | ```typescript 49 | import { Byte, Num, Add, Sub, Mod } from './ts-8-bit' 50 | 51 | // ------------------------------------------------------------------------ 52 | // 53 | // Use Byte and Num to map between bytes and numbers 54 | // 55 | // ------------------------------------------------------------------------ 56 | 57 | type A = Byte<127> // type A = [1, 1, 1, 1, 1, 1, 1, 0] 58 | 59 | type B = Num<[1, 1, 1, 1, 1, 1, 1, 0]> // type B = 127 60 | 61 | type C = Num< Byte<55> > // type C = 55 62 | 63 | // ------------------------------------------------------------------------ 64 | // 65 | // ... with math done exclusively on bytes ... 66 | // 67 | // ------------------------------------------------------------------------ 68 | 69 | type D = Add< // type D = [0, 1, 1, 1, 1, 1, 1, 1] 70 | [1, 1, 1, 1, 1, 1, 1, 0], 71 | [1, 1, 1, 1, 1, 1, 1, 0] 72 | > 73 | 74 | // ------------------------------------------------------------------------ 75 | // 76 | // ... but can be remapped this way ... 77 | // 78 | // ------------------------------------------------------------------------ 79 | 80 | type E = Num< Add< Byte<127>, Byte<127> > > // type E = 254 81 | 82 | // ------------------------------------------------------------------------ 83 | // 84 | // ... and used for stuff like ... 85 | // 86 | // ------------------------------------------------------------------------ 87 | 88 | type FizzBuzz> = 89 | Mod> extends Byte<0> ? 90 | Mod> extends Byte<0> ? 'fizzbuzz' : 'buzz' : 91 | Mod> extends Byte<0> ? 'fizz' : "" 92 | 93 | type T1 = FizzBuzz< Byte<1> > // "" 94 | type T2 = FizzBuzz< Byte<2> > // "" 95 | type T3 = FizzBuzz< Byte<3> > // "fizz" 96 | type T4 = FizzBuzz< Byte<4> > // "" 97 | type T5 = FizzBuzz< Byte<5> > // "buzz" 98 | type T6 = FizzBuzz< Byte<6> > // "fizz" 99 | // ... etc ... 100 | type T15 = FizzBuzz< Byte<15> > // "fizzbuzz" 101 | 102 | // ------------------------------------------------------------------------ 103 | // 104 | // ... or something more elaborate ... 105 | // 106 | // ------------------------------------------------------------------------ 107 | 108 | type IsPrimeOp< 109 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 110 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 111 | > = Num extends 1 ? true : 112 | Num> extends 0 ? false : 113 | IsPrimeOp>> 114 | 115 | type IsPrime< 116 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 117 | > = IsPrimeOp>> 118 | 119 | type P3 = IsPrime< Byte<3> > // true 120 | type P4 = IsPrime< Byte<4> > // false 121 | type P5 = IsPrime< Byte<5> > // true 122 | type P6 = IsPrime< Byte<6> > // false 123 | type P7 = IsPrime< Byte<7> > // true 124 | type P8 = IsPrime< Byte<8> > // false 125 | type P9 = IsPrime< Byte<9> > // false 126 | type P10 = IsPrime< Byte<10> > // false 127 | type P11 = IsPrime< Byte<11> > // true 128 | type P12 = IsPrime< Byte<12> > // false 129 | type P13 = IsPrime< Byte<13> > // true 130 | type P14 = IsPrime< Byte<14> > // false 131 | type P15 = IsPrime< Byte<15> > // false 132 | type P16 = IsPrime< Byte<16> > // false 133 | type P17 = IsPrime< Byte<17> > // true - pushing recursion limits here 134 | type P18 = IsPrime< Byte<18> > // false 135 | 136 | ``` 137 | -------------------------------------------------------------------------------- /src/ts-8-bit.ts: -------------------------------------------------------------------------------- 1 | // ------------------------------------------------------------------------- 2 | // _______ _____ ___ _ _ _ 3 | // |__ __/ ____| / _ \ | | (_) | 4 | // | | | (___ _____| (_) |_____| |__ _| |_ 5 | // | | \___ \______> _ <______| '_ \| | __| 6 | // | | ____) | | (_) | | |_) | | |_ 7 | // |_| |_____/ \___/ |_.__/|_|\__| 8 | // 9 | // Using the TypeScript type system for 8-bit arithmetic 10 | // 11 | // sinclair 2020 | MIT 12 | // 13 | // ------------------------------------------------------------------------- 14 | 15 | // ------------------------------------------------------------------------- 16 | // Bit 17 | // ------------------------------------------------------------------------- 18 | 19 | export type Bit = 0 | 1 20 | 21 | // ------------------------------------------------------------------------- 22 | // Gates 23 | // ------------------------------------------------------------------------- 24 | 25 | export type BitNot = 26 | T extends 0 ? 1 : 27 | T extends 1 ? 0 : 28 | never 29 | 30 | export type BitAnd = 31 | T extends [0, 0] ? 0 : 32 | T extends [0, 1] ? 0 : 33 | T extends [1, 0] ? 0 : 34 | T extends [1, 1] ? 1 : 35 | never 36 | 37 | export type BitOr = 38 | T extends [0, 0] ? 0 : 39 | T extends [0, 1] ? 1 : 40 | T extends [1, 0] ? 1 : 41 | T extends [1, 1] ? 1 : 42 | never 43 | 44 | export type BitXor = 45 | T extends [0, 0] ? 0 : 46 | T extends [0, 1] ? 1 : 47 | T extends [1, 0] ? 1 : 48 | T extends [1, 1] ? 0 : 49 | never 50 | 51 | // ------------------------------------------------------------------------- 52 | // Iterator 53 | // ------------------------------------------------------------------------- 54 | 55 | export type Index = keyof Iterator 56 | export type Prev = Iterator[T][0] 57 | export type Next = Iterator[T][1] 58 | export type Iterator = { 59 | 0: [7, 1], 60 | 1: [0, 2], 61 | 2: [1, 3], 62 | 3: [2, 4], 63 | 4: [3, 5], 64 | 5: [4, 6], 65 | 6: [5, 7], 66 | 7: [6, 0], 67 | } 68 | 69 | // ------------------------------------------------------------------------- 70 | // Bitwise 71 | // ------------------------------------------------------------------------- 72 | 73 | export type Not = [ 74 | BitNot, 75 | BitNot, 76 | BitNot, 77 | BitNot, 78 | BitNot, 79 | BitNot, 80 | BitNot, 81 | BitNot, 82 | ] 83 | 84 | export type And< 85 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 86 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 87 | > = [ 88 | BitAnd<[A[0], B[0]]>, 89 | BitAnd<[A[1], B[1]]>, 90 | BitAnd<[A[2], B[2]]>, 91 | BitAnd<[A[3], B[3]]>, 92 | BitAnd<[A[4], B[4]]>, 93 | BitAnd<[A[5], B[5]]>, 94 | BitAnd<[A[6], B[6]]>, 95 | BitAnd<[A[7], B[7]]>, 96 | ] 97 | 98 | export type Or< 99 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 100 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 101 | > = [ 102 | BitOr<[A[0], B[0]]>, 103 | BitOr<[A[1], B[1]]>, 104 | BitOr<[A[2], B[2]]>, 105 | BitOr<[A[3], B[3]]>, 106 | BitOr<[A[4], B[4]]>, 107 | BitOr<[A[5], B[5]]>, 108 | BitOr<[A[6], B[6]]>, 109 | BitOr<[A[7], B[7]]>, 110 | ] 111 | 112 | export type Xor< 113 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 114 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 115 | > = [ 116 | BitXor<[B[0], A[0]]>, 117 | BitXor<[B[1], A[1]]>, 118 | BitXor<[B[2], A[2]]>, 119 | BitXor<[B[3], A[3]]>, 120 | BitXor<[B[4], A[4]]>, 121 | BitXor<[B[5], A[5]]>, 122 | BitXor<[B[6], A[6]]>, 123 | BitXor<[B[7], A[7]]>, 124 | ] 125 | 126 | export type Rsh = 127 | I extends 0 ? T : Rsh<[T[1], T[2], T[3], T[4], T[5], T[6], T[7], 0], Prev> 128 | 129 | export type Lsh = 130 | I extends 0 ? T : Lsh<[0, T[0], T[1], T[2], T[3], T[4], T[5], T[6]], Prev> 131 | 132 | 133 | // ------------------------------------------------------------------------- 134 | // Equality 135 | // ------------------------------------------------------------------------- 136 | 137 | export type Eq< 138 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 139 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 140 | > = A extends B ? true : false 141 | 142 | export type LtOp< 143 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 144 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 145 | I extends Index 146 | > = A extends B ? false : 147 | [A[I], B[I]] extends [0, 0] ? LtOp> : 148 | [A[I], B[I]] extends [1, 1] ? LtOp> : 149 | [A[I], B[I]] extends [1, 0] ? false : 150 | [A[I], B[I]] extends [0, 1] ? true : 151 | false 152 | 153 | export type Lt< 154 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 155 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 156 | > = LtOp 157 | 158 | export type Lte< 159 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 160 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 161 | > = [Eq, Lt] extends [true, false] ? true : 162 | [Eq, Lt] extends [false, true] ? true : 163 | false 164 | 165 | export type GtOp< 166 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 167 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 168 | I extends Index 169 | > = A extends B ? false : 170 | [A[I], B[I]] extends [0, 0] ? GtOp> : 171 | [A[I], B[I]] extends [1, 1] ? GtOp> : 172 | [A[I], B[I]] extends [0, 1] ? false : 173 | [A[I], B[I]] extends [1, 0] ? true : 174 | false 175 | 176 | export type Gt< 177 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 178 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 179 | > = GtOp 180 | 181 | export type Gte< 182 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 183 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 184 | > = [Eq, Gt] extends [true, false] ? true : 185 | [Eq, Gt] extends [false, true] ? true : 186 | false 187 | 188 | // ------------------------------------------------------------------------- 189 | // Addition 190 | // ------------------------------------------------------------------------- 191 | 192 | export type FullAdder = 193 | T extends [0, 0, 0] ? [0, 0] : 194 | T extends [1, 0, 0] ? [1, 0] : 195 | T extends [0, 1, 0] ? [1, 0] : 196 | T extends [1, 1, 0] ? [0, 1] : 197 | T extends [0, 0, 1] ? [1, 0] : 198 | T extends [1, 0, 1] ? [0, 1] : 199 | T extends [0, 1, 1] ? [0, 1] : 200 | T extends [1, 1, 1] ? [1, 1] : 201 | never 202 | 203 | export type AddOp< 204 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 205 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 206 | I extends Index, 207 | O extends Bit 208 | > = FullAdder<[ 209 | A[I], B[I], 210 | I extends 0 ? 0 : AddOp, 1> 211 | ]>[O] 212 | 213 | export type Add< 214 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 215 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 216 | > = [ 217 | AddOp, 218 | AddOp, 219 | AddOp, 220 | AddOp, 221 | AddOp, 222 | AddOp, 223 | AddOp, 224 | AddOp, 225 | ] 226 | 227 | // ------------------------------------------------------------------------- 228 | // Subtraction 229 | // ------------------------------------------------------------------------- 230 | 231 | export type FullSubtractor = 232 | T extends [0, 0, 0] ? [0, 0] : 233 | T extends [1, 0, 0] ? [1, 0] : 234 | T extends [0, 1, 0] ? [1, 1] : 235 | T extends [1, 1, 0] ? [0, 0] : 236 | T extends [0, 0, 1] ? [1, 1] : 237 | T extends [1, 0, 1] ? [0, 0] : 238 | T extends [0, 1, 1] ? [0, 1] : 239 | T extends [1, 1, 1] ? [1, 1] : 240 | never 241 | 242 | export type SubOp< 243 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 244 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 245 | I extends Index, 246 | O extends Bit 247 | > = FullSubtractor<[ 248 | A[I], B[I], 249 | I extends 0 ? 0 : SubOp, 1> 250 | ]>[O] 251 | 252 | export type Sub< 253 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 254 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 255 | > = [ 256 | SubOp, 257 | SubOp, 258 | SubOp, 259 | SubOp, 260 | SubOp, 261 | SubOp, 262 | SubOp, 263 | SubOp, 264 | ] 265 | 266 | // ------------------------------------------------------------------------- 267 | // Multiply 268 | // ------------------------------------------------------------------------- 269 | 270 | export type MulOp< 271 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 272 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 273 | C extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 274 | D extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 275 | > = Lt extends true ? MulOp, B, Add>, D> : A 276 | 277 | export type Mul< 278 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 279 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 280 | > = MulOp, A, Byte<0>, B> 281 | 282 | // ------------------------------------------------------------------------- 283 | // Division 284 | // ------------------------------------------------------------------------- 285 | 286 | export type DivOp< 287 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 288 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 289 | C extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 290 | > = Gte extends true ? DivOp, B, Add>> : C 291 | 292 | export type Div< 293 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 294 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 295 | > = DivOp> 296 | 297 | // ------------------------------------------------------------------------- 298 | // Modulo 299 | // ------------------------------------------------------------------------- 300 | 301 | export type ModOp< 302 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 303 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 304 | C extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 305 | > = Gte extends true ? ModOp, B, Add>> : A 306 | 307 | export type Mod< 308 | A extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit], 309 | B extends [Bit, Bit, Bit, Bit, Bit, Bit, Bit, Bit] 310 | > = ModOp> 311 | 312 | // ------------------------------------------------------------------------- 313 | // Converters 314 | // ------------------------------------------------------------------------- 315 | 316 | type Size1 = [0] 317 | type Size2 = [...Size1, ...Size1] 318 | type Size3 = [...Size2, ...Size2] 319 | type Size4 = [...Size3, ...Size3] 320 | type Size5 = [...Size4, ...Size4] 321 | type Size6 = [...Size5, ...Size5] 322 | type Size7 = [...Size6, ...Size6] 323 | type Size8 = [...Size7, ...Size7] 324 | export type Num = [ 325 | ...T[0] extends 1 ? Size1 : [], 326 | ...T[1] extends 1 ? Size2 : [], 327 | ...T[2] extends 1 ? Size3 : [], 328 | ...T[3] extends 1 ? Size4 : [], 329 | ...T[4] extends 1 ? Size5 : [], 330 | ...T[5] extends 1 ? Size6 : [], 331 | ...T[6] extends 1 ? Size7 : [], 332 | ...T[7] extends 1 ? Size8 : [], 333 | ]['length'] 334 | 335 | export type Byte = 336 | T extends 0 ? [0, 0, 0, 0, 0, 0, 0, 0] : 337 | T extends 1 ? [1, 0, 0, 0, 0, 0, 0, 0] : 338 | T extends 2 ? [0, 1, 0, 0, 0, 0, 0, 0] : 339 | T extends 3 ? [1, 1, 0, 0, 0, 0, 0, 0] : 340 | T extends 4 ? [0, 0, 1, 0, 0, 0, 0, 0] : 341 | T extends 5 ? [1, 0, 1, 0, 0, 0, 0, 0] : 342 | T extends 6 ? [0, 1, 1, 0, 0, 0, 0, 0] : 343 | T extends 7 ? [1, 1, 1, 0, 0, 0, 0, 0] : 344 | T extends 8 ? [0, 0, 0, 1, 0, 0, 0, 0] : 345 | T extends 9 ? [1, 0, 0, 1, 0, 0, 0, 0] : 346 | T extends 10 ? [0, 1, 0, 1, 0, 0, 0, 0] : 347 | T extends 11 ? [1, 1, 0, 1, 0, 0, 0, 0] : 348 | T extends 12 ? [0, 0, 1, 1, 0, 0, 0, 0] : 349 | T extends 13 ? [1, 0, 1, 1, 0, 0, 0, 0] : 350 | T extends 14 ? [0, 1, 1, 1, 0, 0, 0, 0] : 351 | T extends 15 ? [1, 1, 1, 1, 0, 0, 0, 0] : 352 | T extends 16 ? [0, 0, 0, 0, 1, 0, 0, 0] : 353 | T extends 17 ? [1, 0, 0, 0, 1, 0, 0, 0] : 354 | T extends 18 ? [0, 1, 0, 0, 1, 0, 0, 0] : 355 | T extends 19 ? [1, 1, 0, 0, 1, 0, 0, 0] : 356 | T extends 20 ? [0, 0, 1, 0, 1, 0, 0, 0] : 357 | T extends 21 ? [1, 0, 1, 0, 1, 0, 0, 0] : 358 | T extends 22 ? [0, 1, 1, 0, 1, 0, 0, 0] : 359 | T extends 23 ? [1, 1, 1, 0, 1, 0, 0, 0] : 360 | T extends 24 ? [0, 0, 0, 1, 1, 0, 0, 0] : 361 | T extends 25 ? [1, 0, 0, 1, 1, 0, 0, 0] : 362 | T extends 26 ? [0, 1, 0, 1, 1, 0, 0, 0] : 363 | T extends 27 ? [1, 1, 0, 1, 1, 0, 0, 0] : 364 | T extends 28 ? [0, 0, 1, 1, 1, 0, 0, 0] : 365 | T extends 29 ? [1, 0, 1, 1, 1, 0, 0, 0] : 366 | T extends 30 ? [0, 1, 1, 1, 1, 0, 0, 0] : 367 | T extends 31 ? [1, 1, 1, 1, 1, 0, 0, 0] : 368 | T extends 32 ? [0, 0, 0, 0, 0, 1, 0, 0] : 369 | T extends 33 ? [1, 0, 0, 0, 0, 1, 0, 0] : 370 | T extends 34 ? [0, 1, 0, 0, 0, 1, 0, 0] : 371 | T extends 35 ? [1, 1, 0, 0, 0, 1, 0, 0] : 372 | T extends 36 ? [0, 0, 1, 0, 0, 1, 0, 0] : 373 | T extends 37 ? [1, 0, 1, 0, 0, 1, 0, 0] : 374 | T extends 38 ? [0, 1, 1, 0, 0, 1, 0, 0] : 375 | T extends 39 ? [1, 1, 1, 0, 0, 1, 0, 0] : 376 | T extends 40 ? [0, 0, 0, 1, 0, 1, 0, 0] : 377 | T extends 41 ? [1, 0, 0, 1, 0, 1, 0, 0] : 378 | T extends 42 ? [0, 1, 0, 1, 0, 1, 0, 0] : 379 | T extends 43 ? [1, 1, 0, 1, 0, 1, 0, 0] : 380 | T extends 44 ? [0, 0, 1, 1, 0, 1, 0, 0] : 381 | T extends 45 ? [1, 0, 1, 1, 0, 1, 0, 0] : 382 | T extends 46 ? [0, 1, 1, 1, 0, 1, 0, 0] : 383 | T extends 47 ? [1, 1, 1, 1, 0, 1, 0, 0] : 384 | T extends 48 ? [0, 0, 0, 0, 1, 1, 0, 0] : 385 | T extends 49 ? [1, 0, 0, 0, 1, 1, 0, 0] : 386 | T extends 50 ? [0, 1, 0, 0, 1, 1, 0, 0] : 387 | T extends 51 ? [1, 1, 0, 0, 1, 1, 0, 0] : 388 | T extends 52 ? [0, 0, 1, 0, 1, 1, 0, 0] : 389 | T extends 53 ? [1, 0, 1, 0, 1, 1, 0, 0] : 390 | T extends 54 ? [0, 1, 1, 0, 1, 1, 0, 0] : 391 | T extends 55 ? [1, 1, 1, 0, 1, 1, 0, 0] : 392 | T extends 56 ? [0, 0, 0, 1, 1, 1, 0, 0] : 393 | T extends 57 ? [1, 0, 0, 1, 1, 1, 0, 0] : 394 | T extends 58 ? [0, 1, 0, 1, 1, 1, 0, 0] : 395 | T extends 59 ? [1, 1, 0, 1, 1, 1, 0, 0] : 396 | T extends 60 ? [0, 0, 1, 1, 1, 1, 0, 0] : 397 | T extends 61 ? [1, 0, 1, 1, 1, 1, 0, 0] : 398 | T extends 62 ? [0, 1, 1, 1, 1, 1, 0, 0] : 399 | T extends 63 ? [1, 1, 1, 1, 1, 1, 0, 0] : 400 | T extends 64 ? [0, 0, 0, 0, 0, 0, 1, 0] : 401 | T extends 65 ? [1, 0, 0, 0, 0, 0, 1, 0] : 402 | T extends 66 ? [0, 1, 0, 0, 0, 0, 1, 0] : 403 | T extends 67 ? [1, 1, 0, 0, 0, 0, 1, 0] : 404 | T extends 68 ? [0, 0, 1, 0, 0, 0, 1, 0] : 405 | T extends 69 ? [1, 0, 1, 0, 0, 0, 1, 0] : 406 | T extends 70 ? [0, 1, 1, 0, 0, 0, 1, 0] : 407 | T extends 71 ? [1, 1, 1, 0, 0, 0, 1, 0] : 408 | T extends 72 ? [0, 0, 0, 1, 0, 0, 1, 0] : 409 | T extends 73 ? [1, 0, 0, 1, 0, 0, 1, 0] : 410 | T extends 74 ? [0, 1, 0, 1, 0, 0, 1, 0] : 411 | T extends 75 ? [1, 1, 0, 1, 0, 0, 1, 0] : 412 | T extends 76 ? [0, 0, 1, 1, 0, 0, 1, 0] : 413 | T extends 77 ? [1, 0, 1, 1, 0, 0, 1, 0] : 414 | T extends 78 ? [0, 1, 1, 1, 0, 0, 1, 0] : 415 | T extends 79 ? [1, 1, 1, 1, 0, 0, 1, 0] : 416 | T extends 80 ? [0, 0, 0, 0, 1, 0, 1, 0] : 417 | T extends 81 ? [1, 0, 0, 0, 1, 0, 1, 0] : 418 | T extends 82 ? [0, 1, 0, 0, 1, 0, 1, 0] : 419 | T extends 83 ? [1, 1, 0, 0, 1, 0, 1, 0] : 420 | T extends 84 ? [0, 0, 1, 0, 1, 0, 1, 0] : 421 | T extends 85 ? [1, 0, 1, 0, 1, 0, 1, 0] : 422 | T extends 86 ? [0, 1, 1, 0, 1, 0, 1, 0] : 423 | T extends 87 ? [1, 1, 1, 0, 1, 0, 1, 0] : 424 | T extends 88 ? [0, 0, 0, 1, 1, 0, 1, 0] : 425 | T extends 89 ? [1, 0, 0, 1, 1, 0, 1, 0] : 426 | T extends 90 ? [0, 1, 0, 1, 1, 0, 1, 0] : 427 | T extends 91 ? [1, 1, 0, 1, 1, 0, 1, 0] : 428 | T extends 92 ? [0, 0, 1, 1, 1, 0, 1, 0] : 429 | T extends 93 ? [1, 0, 1, 1, 1, 0, 1, 0] : 430 | T extends 94 ? [0, 1, 1, 1, 1, 0, 1, 0] : 431 | T extends 95 ? [1, 1, 1, 1, 1, 0, 1, 0] : 432 | T extends 96 ? [0, 0, 0, 0, 0, 1, 1, 0] : 433 | T extends 97 ? [1, 0, 0, 0, 0, 1, 1, 0] : 434 | T extends 98 ? [0, 1, 0, 0, 0, 1, 1, 0] : 435 | T extends 99 ? [1, 1, 0, 0, 0, 1, 1, 0] : 436 | T extends 100 ? [0, 0, 1, 0, 0, 1, 1, 0] : 437 | T extends 101 ? [1, 0, 1, 0, 0, 1, 1, 0] : 438 | T extends 102 ? [0, 1, 1, 0, 0, 1, 1, 0] : 439 | T extends 103 ? [1, 1, 1, 0, 0, 1, 1, 0] : 440 | T extends 104 ? [0, 0, 0, 1, 0, 1, 1, 0] : 441 | T extends 105 ? [1, 0, 0, 1, 0, 1, 1, 0] : 442 | T extends 106 ? [0, 1, 0, 1, 0, 1, 1, 0] : 443 | T extends 107 ? [1, 1, 0, 1, 0, 1, 1, 0] : 444 | T extends 108 ? [0, 0, 1, 1, 0, 1, 1, 0] : 445 | T extends 109 ? [1, 0, 1, 1, 0, 1, 1, 0] : 446 | T extends 110 ? [0, 1, 1, 1, 0, 1, 1, 0] : 447 | T extends 111 ? [1, 1, 1, 1, 0, 1, 1, 0] : 448 | T extends 112 ? [0, 0, 0, 0, 1, 1, 1, 0] : 449 | T extends 113 ? [1, 0, 0, 0, 1, 1, 1, 0] : 450 | T extends 114 ? [0, 1, 0, 0, 1, 1, 1, 0] : 451 | T extends 115 ? [1, 1, 0, 0, 1, 1, 1, 0] : 452 | T extends 116 ? [0, 0, 1, 0, 1, 1, 1, 0] : 453 | T extends 117 ? [1, 0, 1, 0, 1, 1, 1, 0] : 454 | T extends 118 ? [0, 1, 1, 0, 1, 1, 1, 0] : 455 | T extends 119 ? [1, 1, 1, 0, 1, 1, 1, 0] : 456 | T extends 120 ? [0, 0, 0, 1, 1, 1, 1, 0] : 457 | T extends 121 ? [1, 0, 0, 1, 1, 1, 1, 0] : 458 | T extends 122 ? [0, 1, 0, 1, 1, 1, 1, 0] : 459 | T extends 123 ? [1, 1, 0, 1, 1, 1, 1, 0] : 460 | T extends 124 ? [0, 0, 1, 1, 1, 1, 1, 0] : 461 | T extends 125 ? [1, 0, 1, 1, 1, 1, 1, 0] : 462 | T extends 126 ? [0, 1, 1, 1, 1, 1, 1, 0] : 463 | T extends 127 ? [1, 1, 1, 1, 1, 1, 1, 0] : 464 | T extends 128 ? [0, 0, 0, 0, 0, 0, 0, 1] : 465 | T extends 129 ? [1, 0, 0, 0, 0, 0, 0, 1] : 466 | T extends 130 ? [0, 1, 0, 0, 0, 0, 0, 1] : 467 | T extends 131 ? [1, 1, 0, 0, 0, 0, 0, 1] : 468 | T extends 132 ? [0, 0, 1, 0, 0, 0, 0, 1] : 469 | T extends 133 ? [1, 0, 1, 0, 0, 0, 0, 1] : 470 | T extends 134 ? [0, 1, 1, 0, 0, 0, 0, 1] : 471 | T extends 135 ? [1, 1, 1, 0, 0, 0, 0, 1] : 472 | T extends 136 ? [0, 0, 0, 1, 0, 0, 0, 1] : 473 | T extends 137 ? [1, 0, 0, 1, 0, 0, 0, 1] : 474 | T extends 138 ? [0, 1, 0, 1, 0, 0, 0, 1] : 475 | T extends 139 ? [1, 1, 0, 1, 0, 0, 0, 1] : 476 | T extends 140 ? [0, 0, 1, 1, 0, 0, 0, 1] : 477 | T extends 141 ? [1, 0, 1, 1, 0, 0, 0, 1] : 478 | T extends 142 ? [0, 1, 1, 1, 0, 0, 0, 1] : 479 | T extends 143 ? [1, 1, 1, 1, 0, 0, 0, 1] : 480 | T extends 144 ? [0, 0, 0, 0, 1, 0, 0, 1] : 481 | T extends 145 ? [1, 0, 0, 0, 1, 0, 0, 1] : 482 | T extends 146 ? [0, 1, 0, 0, 1, 0, 0, 1] : 483 | T extends 147 ? [1, 1, 0, 0, 1, 0, 0, 1] : 484 | T extends 148 ? [0, 0, 1, 0, 1, 0, 0, 1] : 485 | T extends 149 ? [1, 0, 1, 0, 1, 0, 0, 1] : 486 | T extends 150 ? [0, 1, 1, 0, 1, 0, 0, 1] : 487 | T extends 151 ? [1, 1, 1, 0, 1, 0, 0, 1] : 488 | T extends 152 ? [0, 0, 0, 1, 1, 0, 0, 1] : 489 | T extends 153 ? [1, 0, 0, 1, 1, 0, 0, 1] : 490 | T extends 154 ? [0, 1, 0, 1, 1, 0, 0, 1] : 491 | T extends 155 ? [1, 1, 0, 1, 1, 0, 0, 1] : 492 | T extends 156 ? [0, 0, 1, 1, 1, 0, 0, 1] : 493 | T extends 157 ? [1, 0, 1, 1, 1, 0, 0, 1] : 494 | T extends 158 ? [0, 1, 1, 1, 1, 0, 0, 1] : 495 | T extends 159 ? [1, 1, 1, 1, 1, 0, 0, 1] : 496 | T extends 160 ? [0, 0, 0, 0, 0, 1, 0, 1] : 497 | T extends 161 ? [1, 0, 0, 0, 0, 1, 0, 1] : 498 | T extends 162 ? [0, 1, 0, 0, 0, 1, 0, 1] : 499 | T extends 163 ? [1, 1, 0, 0, 0, 1, 0, 1] : 500 | T extends 164 ? [0, 0, 1, 0, 0, 1, 0, 1] : 501 | T extends 165 ? [1, 0, 1, 0, 0, 1, 0, 1] : 502 | T extends 166 ? [0, 1, 1, 0, 0, 1, 0, 1] : 503 | T extends 167 ? [1, 1, 1, 0, 0, 1, 0, 1] : 504 | T extends 168 ? [0, 0, 0, 1, 0, 1, 0, 1] : 505 | T extends 169 ? [1, 0, 0, 1, 0, 1, 0, 1] : 506 | T extends 170 ? [0, 1, 0, 1, 0, 1, 0, 1] : 507 | T extends 171 ? [1, 1, 0, 1, 0, 1, 0, 1] : 508 | T extends 172 ? [0, 0, 1, 1, 0, 1, 0, 1] : 509 | T extends 173 ? [1, 0, 1, 1, 0, 1, 0, 1] : 510 | T extends 174 ? [0, 1, 1, 1, 0, 1, 0, 1] : 511 | T extends 175 ? [1, 1, 1, 1, 0, 1, 0, 1] : 512 | T extends 176 ? [0, 0, 0, 0, 1, 1, 0, 1] : 513 | T extends 177 ? [1, 0, 0, 0, 1, 1, 0, 1] : 514 | T extends 178 ? [0, 1, 0, 0, 1, 1, 0, 1] : 515 | T extends 179 ? [1, 1, 0, 0, 1, 1, 0, 1] : 516 | T extends 180 ? [0, 0, 1, 0, 1, 1, 0, 1] : 517 | T extends 181 ? [1, 0, 1, 0, 1, 1, 0, 1] : 518 | T extends 182 ? [0, 1, 1, 0, 1, 1, 0, 1] : 519 | T extends 183 ? [1, 1, 1, 0, 1, 1, 0, 1] : 520 | T extends 184 ? [0, 0, 0, 1, 1, 1, 0, 1] : 521 | T extends 185 ? [1, 0, 0, 1, 1, 1, 0, 1] : 522 | T extends 186 ? [0, 1, 0, 1, 1, 1, 0, 1] : 523 | T extends 187 ? [1, 1, 0, 1, 1, 1, 0, 1] : 524 | T extends 188 ? [0, 0, 1, 1, 1, 1, 0, 1] : 525 | T extends 189 ? [1, 0, 1, 1, 1, 1, 0, 1] : 526 | T extends 190 ? [0, 1, 1, 1, 1, 1, 0, 1] : 527 | T extends 191 ? [1, 1, 1, 1, 1, 1, 0, 1] : 528 | T extends 192 ? [0, 0, 0, 0, 0, 0, 1, 1] : 529 | T extends 193 ? [1, 0, 0, 0, 0, 0, 1, 1] : 530 | T extends 194 ? [0, 1, 0, 0, 0, 0, 1, 1] : 531 | T extends 195 ? [1, 1, 0, 0, 0, 0, 1, 1] : 532 | T extends 196 ? [0, 0, 1, 0, 0, 0, 1, 1] : 533 | T extends 197 ? [1, 0, 1, 0, 0, 0, 1, 1] : 534 | T extends 198 ? [0, 1, 1, 0, 0, 0, 1, 1] : 535 | T extends 199 ? [1, 1, 1, 0, 0, 0, 1, 1] : 536 | T extends 200 ? [0, 0, 0, 1, 0, 0, 1, 1] : 537 | T extends 201 ? [1, 0, 0, 1, 0, 0, 1, 1] : 538 | T extends 202 ? [0, 1, 0, 1, 0, 0, 1, 1] : 539 | T extends 203 ? [1, 1, 0, 1, 0, 0, 1, 1] : 540 | T extends 204 ? [0, 0, 1, 1, 0, 0, 1, 1] : 541 | T extends 205 ? [1, 0, 1, 1, 0, 0, 1, 1] : 542 | T extends 206 ? [0, 1, 1, 1, 0, 0, 1, 1] : 543 | T extends 207 ? [1, 1, 1, 1, 0, 0, 1, 1] : 544 | T extends 208 ? [0, 0, 0, 0, 1, 0, 1, 1] : 545 | T extends 209 ? [1, 0, 0, 0, 1, 0, 1, 1] : 546 | T extends 210 ? [0, 1, 0, 0, 1, 0, 1, 1] : 547 | T extends 211 ? [1, 1, 0, 0, 1, 0, 1, 1] : 548 | T extends 212 ? [0, 0, 1, 0, 1, 0, 1, 1] : 549 | T extends 213 ? [1, 0, 1, 0, 1, 0, 1, 1] : 550 | T extends 214 ? [0, 1, 1, 0, 1, 0, 1, 1] : 551 | T extends 215 ? [1, 1, 1, 0, 1, 0, 1, 1] : 552 | T extends 216 ? [0, 0, 0, 1, 1, 0, 1, 1] : 553 | T extends 217 ? [1, 0, 0, 1, 1, 0, 1, 1] : 554 | T extends 218 ? [0, 1, 0, 1, 1, 0, 1, 1] : 555 | T extends 219 ? [1, 1, 0, 1, 1, 0, 1, 1] : 556 | T extends 220 ? [0, 0, 1, 1, 1, 0, 1, 1] : 557 | T extends 221 ? [1, 0, 1, 1, 1, 0, 1, 1] : 558 | T extends 222 ? [0, 1, 1, 1, 1, 0, 1, 1] : 559 | T extends 223 ? [1, 1, 1, 1, 1, 0, 1, 1] : 560 | T extends 224 ? [0, 0, 0, 0, 0, 1, 1, 1] : 561 | T extends 225 ? [1, 0, 0, 0, 0, 1, 1, 1] : 562 | T extends 226 ? [0, 1, 0, 0, 0, 1, 1, 1] : 563 | T extends 227 ? [1, 1, 0, 0, 0, 1, 1, 1] : 564 | T extends 228 ? [0, 0, 1, 0, 0, 1, 1, 1] : 565 | T extends 229 ? [1, 0, 1, 0, 0, 1, 1, 1] : 566 | T extends 230 ? [0, 1, 1, 0, 0, 1, 1, 1] : 567 | T extends 231 ? [1, 1, 1, 0, 0, 1, 1, 1] : 568 | T extends 232 ? [0, 0, 0, 1, 0, 1, 1, 1] : 569 | T extends 233 ? [1, 0, 0, 1, 0, 1, 1, 1] : 570 | T extends 234 ? [0, 1, 0, 1, 0, 1, 1, 1] : 571 | T extends 235 ? [1, 1, 0, 1, 0, 1, 1, 1] : 572 | T extends 236 ? [0, 0, 1, 1, 0, 1, 1, 1] : 573 | T extends 237 ? [1, 0, 1, 1, 0, 1, 1, 1] : 574 | T extends 238 ? [0, 1, 1, 1, 0, 1, 1, 1] : 575 | T extends 239 ? [1, 1, 1, 1, 0, 1, 1, 1] : 576 | T extends 240 ? [0, 0, 0, 0, 1, 1, 1, 1] : 577 | T extends 241 ? [1, 0, 0, 0, 1, 1, 1, 1] : 578 | T extends 242 ? [0, 1, 0, 0, 1, 1, 1, 1] : 579 | T extends 243 ? [1, 1, 0, 0, 1, 1, 1, 1] : 580 | T extends 244 ? [0, 0, 1, 0, 1, 1, 1, 1] : 581 | T extends 245 ? [1, 0, 1, 0, 1, 1, 1, 1] : 582 | T extends 246 ? [0, 1, 1, 0, 1, 1, 1, 1] : 583 | T extends 247 ? [1, 1, 1, 0, 1, 1, 1, 1] : 584 | T extends 248 ? [0, 0, 0, 1, 1, 1, 1, 1] : 585 | T extends 249 ? [1, 0, 0, 1, 1, 1, 1, 1] : 586 | T extends 250 ? [0, 1, 0, 1, 1, 1, 1, 1] : 587 | T extends 251 ? [1, 1, 0, 1, 1, 1, 1, 1] : 588 | T extends 252 ? [0, 0, 1, 1, 1, 1, 1, 1] : 589 | T extends 253 ? [1, 0, 1, 1, 1, 1, 1, 1] : 590 | T extends 254 ? [0, 1, 1, 1, 1, 1, 1, 1] : 591 | T extends 255 ? [1, 1, 1, 1, 1, 1, 1, 1] : 592 | never --------------------------------------------------------------------------------