Class new title: LargeInteger subclassof: Number fields: 'bytes "A Natural number (digits are 0 to 255)" neg "The sign" ' declare: ''; sharing: BitMasks  t1 [(self compare: t1) < 3  [self] false]  t1 [(self compare: t1) > 1  [self] false] * t1 | t2 t3 [t2 _ t1 neg. t3 _ bytes nattimes: t1 bytes. LargeInteger new bytes: t3 neg: neg  t2  false] + t1 | t2 t3 [t2 _ t1 neg. neg  t2  [t3 _ bytes natadd: t1 bytes. LargeInteger new bytes: t3 neg: neg] t3 _ bytes natsubtract: t1 bytes. neg  [t3 negate] t3] - t1 | t2 t3 [t2 _ t1 neg. neg  t2  [t3 _ bytes natsubtract: t1 bytes. neg  [t3 neg_ t3 neg  false] t3] t3 _ bytes natadd: t1 bytes. LargeInteger new bytes: t3 neg: neg] / t1 [((self intdiv: t1)  1) asInteger] < t1 [(self compare: t1) = 1  [self] false] = t1 [t1 isNumber  [(self compare: t1) = 2  [self] false] false] > t1 [(self compare: t1) = 3  [self] false] \ t1 [(self intdiv: t1)  2] abs [LargeInteger new bytes: bytes neg: false] allmask: t1 [t1 = (self land: t1)] anymask: t1 [0  (self land: t1)] asFloat [self asString asFloat] asInt16 | t1 [t1 _ bytes  1. [bytes length > 1  [t1 _ (bytes  2 lshift: 8) + t1]]. neg  [t1 = 0 minVal  [t1] 0 - t1] t1] asInteger [self canBeSmall  [self asSmall] self] asLarge asSmall | t1 t2 [t1 _ bytes  1. [bytes length > 1  [t2 _ bytes  2. t1 _ ([NoteTaker  [(t2 land: 63) - (t2 land: 64)] t2] lshift: 8) + t1]]. neg  [t1 = 0 minVal  [t1] 0 - t1] t1] bit: t1 | t2 [t2 _ bytes  (1 + (t1 - 1 / 8)). (t2 lshift: 0 - (t1 - 1 \ 8)) land: 1] bits: t1 to: t2 | t3 t4 [t3 _ t2 - t1 + 1. t4 _ [t3 < 15  [biton  (t3 + 1) - 1] t3 = 15  [32767] 1]. (self lshift: t2 - 15) land: t4] bits: t1 to: t2 _ t3 | t4 t5 [t4 _ t2 - t1 + 1. t5 _ [t4 < 15  [biton  (t4 + 1) - 1] t4 = 15  [32767] 1]. (self land: ((t5 lshift: 15 - t2) lxor: 1)) lor: ((t3 land: t5) lshift: 15 - t2)] bytes [bytes] bytes: t1 neg: t2 [bytes _ t1. neg _ t2. while bytes isLarge do [bytes _ bytes bytes]] canBeInt16 | t1 [[bytes length > 2  [for t1 from: 3 to: bytes length do [bytes  t1  0  [false]]]]. self  32767 and self  32768  [true] false] canBeSmall | t1 [[bytes length > 2  [for t1 from: 3 to: bytes length do [bytes  t1  0  [false]]]]. self  1 maxVal and self  1 minVal  [true] false] compare: t1 | t2 t3 [((bytes length = 1 and bytes  1 = 0) and t1 bytes length = 1) and t1 bytes  1 = 0  [2] neg  [t1 neg  [t1 bytes natcompare: bytes] 1] t1 neg  [3] bytes natcompare: t1 bytes] even [(bytes  1 land: 1) = 0] field: t1 | t2 [t2 _ 15 - (t1 land: 15). self bits: t2 - (t1 / 16) + 1 to: t2] field: t1 _ t2 | t3 [t3 _ 15 - (t1 land: 15). self bits: t3 - (t1 / 16) + 1 to: t3 _ t2] hibit [bytes last hibit + (8 * (bytes length - 1))] intdiv: t1 | t2 t3 t4 t5 t6 [t5 _ bytes natdiv: t1 bytes. t2 _ t5  1. t3 _ (t5  2) asInteger. t4 _ neg  t1 neg  false. [t2 last = 0  [t2 length < 2  [] t2 _ t2 growby: 1]]. t5  1 _ LargeInteger new bytes: t2 neg: t4. t5  2 _ [t4 and 0  t3  [t1 abs - t3] t3]. t5] isLarge land: t1 | t2 t3 t4 [NoteTaker  [t2 _ self logicArg bytes. t3 _ t1 asLarge logicArg bytes. t4 _ Natural new: 2. t4  1 _ t2  1 land: t3  1. t4  2 _ t2  2 land: t3  2. (LargeInteger new bytes: t4 neg: false) logicUnArg] self asSmall land: t1] logicArg | t1 t2 t3 [neg  [t1 _ Natural new: 2. t2 _ (bytes  1 lxor: 255) + 1. t1  1 _ t2 land: 255. t2 _ t2 lshift: 8. t1  2 _ (bytes  2 lxor: 255) + t2 land: 255. LargeInteger new bytes: t1 neg: false]] logicUnArg | t1 [bytes length  2  [user notify: 'not logic arg'] (bytes  2 land: 128)  0  [t1 _ (bytes  1 lxor: 255) + 1. bytes  1 _ t1 land: 255. t1 _ t1 lshift: 8. bytes  2 _ (bytes  2 lxor: 255) + t1 land: 255. neg _ true] self asInt16] lor: t1 | t2 t3 t4 [NoteTaker  [t2 _ self logicArg. t3 _ t1 asLarge logicArg. t4 _ Natural new: 2. t4  1 _ t2  1 lor: t3  1. t4  2 _ t2  2 lor: t3  2. (LargeInteger new bytes: t4 neg: false) logicUnArg] self asSmall lor: t1] lshift: t1 | t2 t3 t4 t5 t6 [NoteTaker  [t2 _ self logicArg. t3 _ Natural new: 2. t3  1 _ t2 bytes  1. t3  2 _ t2 bytes  2. t2 bytes: t3 neg: false. t6 _ t1 abs. t6  16  [0] t4 _ t6 / 2. t5 _ t6 - t4. t4 _ 1 lshift: t4. t5 _ 1 lshift: t5. t6 _ t4 asLarge * t5 asLarge. t2 _ [t1 < 0  [t2 / t6] t2 * t6]. t3 _ Natural new: 2. t3  1 _ t2 bytes  1. t3  2 _ t2 bytes  2. (LargeInteger new bytes: t3 neg: false) logicUnArg] self asSmall lshift: t1] ltand: t1 | t2 t3 t4 [t2 _ self logicArg bytes. t3 _ t1 asLarge logicArg bytes. t4 _ Natural new: 2. t4  1 _ t2  1 land: t3  1. t4  2 _ t2  2 land: t3  2. (LargeInteger new bytes: t4 neg: false) logicUnArg] lxor: t1 | t2 t3 t4 [NoteTaker  [t2 _ self logicArg. t3 _ t1 asLarge logicArg. t4 _ Natural new: 2. t4  1 _ t2  1 lxor: t3  1. t4  2 _ t2  2 lxor: t3  2. (LargeInteger new bytes: t4 neg: false) logicUnArg] self asSmall lxor: t1] neg [neg] neg_ t1 [neg _ t1] negate [LargeInteger new bytes: bytes neg: neg  false] negated [LargeInteger new bytes: bytes neg: neg  false] nomask: t1 [0 = (self land: t1)] printon: t1 base: t2 [[neg  [t1 append: '']]. bytes printon: t1 base: t2]