Re: GrayCode to Binary conversion
- Posted by petelomax at blueyonder.co.uk Nov 16, 2002
- 381 views
On Fri, 15 Nov 2002 19:34:33 -0500, euman at bellsouth.net wrote:>Hello all,>>Would someone please tell me if my GrayCode to Binary routine is proper.>The test Ive done so far seem to be accurate but I need further opinion.Using my limited understanding, I cobbled the following test together.I've proved there are no jumps or clashes before 10,000; you mightwant to leave it running overnight.function Gray2Bin(atom Num) atom junk junk = floor(Num/2) return xor_bits(Num, junk)end functionfunction binary(integer anumber)sequence result result={} while anumber do if and_bits(anumber,1) then --odd result="1"&result anumber-=1 else result="0"&result end if anumber/=2 end while return resultend functioninteger i, diffs, comparefromsequence result,prevresultprevresult=binary(Gray2Bin(1)) -- "1"i=2 comparefrom=2 -- "11"atom aa=time()+1while 1 do result=binary(Gray2Bin(i)) if length(result)=length(prevresult) then diffs=0 for k=1 to length(result) do if result[k]!=prevresult[k] then diffs+=1 end if end for if diffs!=1 then ?9/0 end if for k=comparefrom to i-1 do if equal(result,binary(Gray2Bin(k))) then ?9/0 end if end for else if not equal(result[2..length(result)],prevresult) then ?9/0 end if comparefrom=i end if prevresult=result if a<time() then printf(1,"%d %s\n",{i,result}) a=time()+1 end if i+=1end whilePete