; Unary-to-Binary TM ;----------------------------------------------------------------- ; A Turing machine for converting unary numbers to binary numbers ; by repeatedly calling the BinaryAdd1 machine as a "subroutine" ; ; INPUT: a block of 1's representing a number written in unary ; OUTPUT: a block of 0's and 1's representing the number in binary ;----------------------------------------------------------------- 0 * * * m1 ; Starting state is m1 m1 1 1 L m1 ; Write 0 to the left of the unary string, with an m1 _ _ L m2 ; intervening space, then move back to the beginning m2 _ 0 R m3 ; of the unary string and switch to state m4. m3 _ _ R m4 m4 1 x R m4 ; Replace all unary 1's by x's and move to the m4 _ _ L m5 ; rightmost x, then switch to state m5. m5 x _ L m6 ; Erase the rightmost x. m6 x x L m6 ; Scan leftward over the x's, and then continue m6 _ _ L m7 ; scanning left to the beginning of the binary m7 0 0 L m7 ; string of 0's and 1's, stopping on the leftmost m7 1 1 L m7 ; symbol. Then switch to state s1 to add 1 to m7 _ _ R s1 ; the binary string. s1 0 0 R s1 ; Binary Add1 subroutine s1 1 1 R s1 s1 _ _ L s2 s2 0 1 * s3 s2 1 0 L s2 s2 _ 1 * s3 s3 0 0 L s3 s3 1 1 L s3 s3 _ _ R m8 ; After adding 1, switch to state m8. m8 0 0 R m8 ; Scan rightward over the binary string of 0's and m8 1 1 R m8 ; 1's, and then continue scanning rightward to find m8 _ _ R m9 ; the end of the string of x's. m9 x x R m9 m9 _ _ L m5 ; Go back to state m5 to continue the loop... m5 _ _ * halt ; Halt when no more x's are found in state m5.