32 lines
697 B
Plaintext
32 lines
697 B
Plaintext
#proto GrayEncode(_X_)
|
|
#synon _GrayEncode *getGrayEncode
|
|
#proto GrayDecode(_X_)
|
|
#synon _GrayDecode *getGrayDecode
|
|
|
|
#include <hbasic.h>
|
|
|
|
Begin
|
|
Gray=0
|
|
SizeBin(4) // size 5 bits: 0->4
|
|
Take (" # BINARY GRAY DECODE\n")
|
|
Take ("------------------------------\n"), and Print It
|
|
For Up( i := 0, 31, 1)
|
|
Print( LPad$(" ",2,Str$(i))," => ", Bin$(i)," => ")
|
|
get Gray Encode(i) and Copy to (Gray), get Binary; then Take(" => ")
|
|
now get Gray Decode( Gray ), get Binary, and Print It with a Newl
|
|
Next
|
|
End
|
|
|
|
Subrutines
|
|
|
|
Gray Encode(n)
|
|
Return (XorBit( RShift(1,n), n ))
|
|
|
|
Gray Decode(n)
|
|
p = n
|
|
While ( n )
|
|
n >>= 1
|
|
p != n
|
|
Wend
|
|
Return (p)
|