0
$\begingroup$

I have been given the problem of writing a turing machine with the commands:

if, while, whileNot, read X, write X, goLeft, goRight, HALT

The problem was simply "calculate a two's complement number, from number with \$ on the left and # on the right, ex: $10110100#"

I started by writing this pseudocode to flip the bits:

whileNot read #
    if read 1
        write 0
    if read 0
        write 1
    goRight

But then I realized once 'write 0' executes, 'if read 0' will immediately be true and undo my work. I need a statement that is effectively 'else if read 0', is there any turing trickery that I can employ to do something like this?

$\endgroup$
1
  • $\begingroup$ Hm, this is borderline between simulation proof and programming. Community votes, please! $\endgroup$
    – Raphael
    Apr 14, 2015 at 15:21

1 Answer 1

0
$\begingroup$

The following code should work:

whileNot read #
    while read 1
        write 0
        goRight
    while read 0
        write 1
        goRight

As for implementing IF-THEN-ELSE in general, it depends on the exact semantics of your programming language, which you haven't specified.

$\endgroup$
1
  • $\begingroup$ Yeah, the general "else implementation" question is probably beyond the scope of my assignment. Your solution is much more elegant than what I was trying. $\endgroup$ Apr 14, 2015 at 15:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.