30 lines
841 B
Plaintext
30 lines
841 B
Plaintext
Module Code32 (&code(), &decode()){
|
|
Const d$="{0::-2} {1:-6} {2:-6} {3:-6} {4::-2}"
|
|
For i=0 to 32
|
|
g=code(i)
|
|
b=decode(g)
|
|
Print format$(d$, i, @bin$(i), @bin$(g), @bin$(b), b)
|
|
Next
|
|
// static function
|
|
Function bin$(a)
|
|
a$=""
|
|
Do n= a mod 2 : a$=if$(n=1->"1", "0")+a$ : a|div 2 : Until a==0
|
|
=a$
|
|
End Function
|
|
}
|
|
Module GrayCode {
|
|
Module doit (&a(), &b()) { }
|
|
Function GrayEncode(a) {
|
|
=binary.xor(a, binary.shift(a,-1))
|
|
}
|
|
Function GrayDecode(a) {
|
|
b=0
|
|
Do b=binary.xor(a, b) : a=binary.shift(a,-1) : Until a==0
|
|
=b
|
|
}
|
|
// pass 2 functions to Code32
|
|
doit &GrayEncode(), &GrayDecode()
|
|
}
|
|
// pass Code32 to GrayCode in place of doit
|
|
GrayCode ; doit as Code32
|