Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-rosetta/blob/main/drafts/rosettacode/gray-code.wiki # omega-rosetta aien-dev/omega-rosetta Brought by drakestapleton Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-rosetta / drafts / rosettacode / gray-code.wiki` 124 lines ยท 2538 bytes Raw Blame History 1 =={{header|Omegese}}== 2 {{works with|omc|sha256 `2c824805`, aien-dev/omega-mv `e5b32567`, Linux AArch64}} 3 Omega has no imports, so the small output helpers (emit and friends) are part of the program. 4 5 om2 6 text nl "\n" 7 text sp " " 8 text minus "-" 9 text thead "number binary gray decoded\n" 10 note emit writes every byte of a to stdout; a short write is retried, an error exits 1 11 fn emit a view uchar * to void 12 o = ulong 0 13 while lt o len a bound 4096 14 k = write 1 a o sub len a o 15 if le k 0 16 exit 1 17 end 18 o = add o ulong k 19 end 20 end 21 note emitu writes n in decimal; 20 digits hold every ulong 22 fn emitu n ulong to void 23 d = new uchar 20 0 24 i = ulong 20 25 v = n 26 while true bound 20 27 i = sub i 1 28 put d i uchar add rem v 10 48 29 v = div v 10 30 if eq v 0 31 break 32 end 33 end 34 emit part d i sub 20 i 35 end 36 note emitw writes n right-aligned in w columns (w at most 20; a wider number is not cut) 37 fn emitw n ulong w ulong to void 38 d = new uchar 20 32 39 i = ulong 20 40 v = n 41 while true bound 20 42 i = sub i 1 43 put d i uchar add rem v 10 48 44 v = div v 10 45 if eq v 0 46 break 47 end 48 end 49 if gt w sub 20 i 50 i = sub 20 w 51 end 52 emit part d i sub 20 i 53 end 54 note bin5 writes the low 5 bits of v, most significant first 55 fn bin5 v ulong to void 56 d = new uchar 5 48 57 for i ulong 5 58 if ne and shr v sub 4 i 1 0 59 put d i 49 60 end 61 end 62 emit d 63 end 64 note encode is n xor (n >> 1); decode undoes it by xoring in every right shift of g 65 fn encode n ulong to ulong 66 ret xor n shr n 1 67 end 68 note one xor per bit of g: a ulong has 64, so 64 shifts empty s 69 fn decode g ulong to ulong 70 n = g 71 s = shr g 1 72 while ne s 0 bound 64 73 n = xor n s 74 s = shr s 1 75 end 76 ret n 77 end 78 fn main to int 79 emit thead 80 for n ulong 32 81 g = encode n 82 emitw n 6 83 emit sp 84 emit sp 85 bin5 n 86 emit sp 87 emit sp 88 emit sp 89 bin5 g 90 emit sp 91 emit sp 92 emitw decode g 7 93 emit nl 94 end 95 ret 0 96 end 97 98 {{out}} 99

100 number binary gray decoded

101 0 00000 00000 0

102 1 00001 00001 1

103 2 00010 00011 2

104 3 00011 00010 3

105 4 00100 00110 4

106 5 00101 00111 5

107 6 00110 00101 6

108 7 00111 00100 7

109 8 01000 01100 8

110 9 01001 01101 9

111 10 01010 01111 10

112 11 01011 01110 11

113 12 01100 01010 12

114 13 01101 01011 13

115 ...

116 24 11000 10100 24

117 25 11001 10101 25

118 26 11010 10111 26

119 27 11011 10110 27

120 28 11100 10010 28

121 29 11101 10011 29

122 30 11110 10001 30

123 31 11111 10000 31

124