Anonymous user
Gray code: Difference between revisions
Updated to compile with Nim 1.4. Changed somewhat the presentation fo result.
Simple9371 (talk | contribs) m (→{{header|Batch File}}: less code, but slower (TnT)) |
(Updated to compile with Nim 1.4. Changed somewhat the presentation fo result.) |
||
Line 3,191:
=={{header|Nim}}==
{{trans|C}}
<lang nim>proc grayEncode(n: int):
n xor (n shr 1)
proc grayDecode(n: int):
result = n
var t = n
Line 3,201:
result = result xor t</lang>
Demonstration code:
<lang nim>import strutils, strformat
for i in 0 .. 32:
echo &"{i
{{out}}
<pre> 0 => 000000 => 0
10 => 001111 => 10
11 => 001110 => 11
Line 3,239 ⟶ 3,238:
30 => 010001 => 30
31 => 010000 => 31
32 => 110000 => 32</pre>
=={{header|OCaml}}==
|