Gray code: Difference between revisions

2,679 bytes added ,  3 years ago
added NOWUT
(added Arturo)
(added NOWUT)
Line 3,299:
31 => 010000 => 31
32 => 110000 => 32</pre>
 
=={{header|NOWUT}}==
adapted from C
<lang NOWUT>; link with PIOxxx.OBJ
 
sectiondata
 
output: db " : "
inbinary: db "00000 => "
graybinary: db "00000 => "
outbinary: db "00000"
db 13,10,0 ; carriage return and null terminator
 
sectioncode
 
start!
gosub initplatform
 
beginfunc
localvar i.d,g.d,b.d
 
i=0
whileless i,32
callex g,gray_encode,i
callex b,gray_decode,g
 
callex ,bin2string,i,inbinary,5 ; 5 = number of binary digits
callex ,bin2string,g,graybinary,5
callex ,bin2string,b,outbinary,5
 
callex ,printhex8,i ; display hex value
; because there is no PIO routine for decimals...
callex ,printnt,output.a
 
i=_+1
wend
 
endfunc
end
 
gray_encode:
beginfunc n.d
n=_ xor (n shr 1)
endfunc n
returnex 4 ; clean off 1 parameter from the stack
 
gray_decode:
beginfunc n.d
localvar p.d
p=n
whilegreater n,1
n=_ shr 1 > p=_ xor n
wend
endfunc p
returnex 4 ; clean off 1 parameter from the stack
 
bin2string:
beginfunc digits.d,straddr.d,value.d
 
whilegreater digits,0
digits=_-1
[straddr].b=value shr digits and 1+$30 ; write an ASCII '0' or '1'
straddr=_+1 ; increment the pointer
wend
 
endfunc
returnex $0C ; clean off 3 parameters from the stack
</lang>
{{out}}
<pre style="overflow: auto; height: 20em;">00 : 00000 => 00000 => 00000
01 : 00001 => 00001 => 00001
02 : 00010 => 00011 => 00010
03 : 00011 => 00010 => 00011
04 : 00100 => 00110 => 00100
05 : 00101 => 00111 => 00101
06 : 00110 => 00101 => 00110
07 : 00111 => 00100 => 00111
08 : 01000 => 01100 => 01000
09 : 01001 => 01101 => 01001
0A : 01010 => 01111 => 01010
0B : 01011 => 01110 => 01011
0C : 01100 => 01010 => 01100
0D : 01101 => 01011 => 01101
0E : 01110 => 01001 => 01110
0F : 01111 => 01000 => 01111
10 : 10000 => 11000 => 10000
11 : 10001 => 11001 => 10001
12 : 10010 => 11011 => 10010
13 : 10011 => 11010 => 10011
14 : 10100 => 11110 => 10100
15 : 10101 => 11111 => 10101
16 : 10110 => 11101 => 10110
17 : 10111 => 11100 => 10111
18 : 11000 => 10100 => 11000
19 : 11001 => 10101 => 11001
1A : 11010 => 10111 => 11010
1B : 11011 => 10110 => 11011
1C : 11100 => 10010 => 11100
1D : 11101 => 10011 => 11101
1E : 11110 => 10001 => 11110
1F : 11111 => 10000 => 11111</pre>
 
=={{header|OCaml}}==
Anonymous user