简体   繁体   中英

Turing machine that adds 2-digit binary numbers

Hey as the title says am trying to create a Turing machine that adds 2 2-digit binary numbers.

Up till now I managed to make it work for the case of 10 + 01 but I can't make it work for all the number combinations. Could anyone help? This is my code so far.

The input format is X NUM1 NUM2 X NUM3 NUM4 (x10x01):

State Read Write Direction NextState
0 X X R 0
0 1 1 R 0
0 0 1 L 1
1 X 0 L 1
1 0 0 L 1
1 1 0 L 2
2 X 0 R 2
2 0 0 R 2
2 1 1 R 0
3 _ _ N HALT

You TM does the following:

  • State 0: move right until we read a 0, then write 1, move left and go to state 1.
  • State 1: move left until we read a 1, then write 0, move right and go to state 2.
  • State 2: move right until we read a 1, move right and go to state 0.
  • State 3 (never reached): Halt

This logic doesn't appear to be adding anything. On input x10x01 it will:

  1. Tape: x10x01 , state 0, head at position 1 ( x ). Move right until we read a 0, write 1, move left and go to state 1.
  2. Tape: x11x01 , state 1, head at position 2 ( 1 ). Move left until we read a 1, then write 0, move right and go to state 2.
  3. Tape: x01x01 , state 2, head at position 3 ( 1 ). Move right until we read a 1, move right and go to state 0.
  4. Tape: x01x01 , state 0, head at position 4 ( x ).
  5. Tape: x01x11 , state 1, head at position 4 ( x ).
  6. Tape: x00x11 , state 2, head at position 4 ( x ).
  7. Tape: x00x11 , state 0, head at position 6 ( 1 ). At this point the machine loops forever or crashes, depending on the specifics of your implementation, as it will read uninitialized tape.

While it might look like the machine added 01 and 10 to get 11 , really it just shuffled 1 s and 0 s around haphazardly.

An actual addition TM would need to do a few more things than your machine is doing. Specifically:

  • Find the least significant unmarked bit of both operands,
  • Mark them,
  • Based on their values, determine the one-bit result and the one-bit carry
  • Write the result, and apply the carry to the next least-significant unmarked bits.
  • Repeat until there are no more digits.

Since you're only worried about two-digit numbers, you can skip marking the digits, and just have additional states to track which bit you're working with.

You could also just hard-code all possible inputs (there are only 16), but that's probably not in the spirit of the homework you've been assigned.

For example, to actually add two one-bit numbers, you might want an algorithm like this:

  • State 0: move right until we read a digit. Write 0. If the digit is 0, go to state 1. If the digit is 1, go to state 2.
  • State 1: move right until we read a digit. If the digit is 0, go to state 3. If the digit is 4, go to state 4.
  • State 2: move right until we read a digit. If the digit is 0, go to state 4. If the digit is 5, go to state 5.
  • State 3: write 0, halt
  • State 4: write 1, halt
  • State 5: write 1, move right go to state 3.

In this example, states 1 and 2 track whether the first number was 0 or 1. States 3, 4 and 5 add the numbers together. State 3 is when both are 0. State 4 is when one is 1. State 5 is when both are 1 (this has a carry).

The corresponding TM might look like this:

State Read Write Direction NewState
0 X X R 0
0 0 0 R 1
0 1 0 R 2
1 X X R 1
1 0 0 N 3
1 1 1 N 4
2 X X R 2
2 0 0 N 4
2 1 1 N 5
3 _ 0 N Halt
4 _ 1 N Halt
5 _ 1 R 3

I'll leave it up to you to figure out how to wire two iterations of this concept together to add two 2-bit numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM