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:
This logic doesn't appear to be adding anything. On input x10x01
it will:
x10x01
, state 0, head at position 1 ( x
). Move right until we read a 0, write 1, move left and go to state 1.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.x01x01
, state 2, head at position 3 ( 1
). Move right until we read a 1, move right and go to state 0.x01x01
, state 0, head at position 4 ( x
).x01x11
, state 1, head at position 4 ( x
).x00x11
, state 2, head at position 4 ( x
).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:
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:
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.