RosettaCodeData/Task/Gray-code/FreeBASIC/gray-code.basic

34 lines
659 B
Plaintext

' version 18-01-2017
' compile with: fbc -s console
Function gray2bin(g As UInteger) As UInteger
Dim As UInteger b = g
While g
g Shr= 1
b Xor= g
Wend
Return b
End Function
Function bin2gray(b As UInteger) As UInteger
Return b Xor (b Shr 1)
End Function
' ------=< MAIN >=------
Dim As UInteger i
Print " i binary gray gra2bin"
Print String(32,"=")
For i = 0 To 31
Print Using "## --> "; i;
print Bin(i,5); " --> ";
Print Bin(bin2gray(i),5); " --> ";
Print Bin(gray2bin(bin2gray(i)),5)
Next
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End