简体   繁体   中英

Turing machine to calculate sum of 2 binary numbers

How can I build a Turing machine to calculate the sum of 2 binaries numbers given X$Y* for input?

For example, suppose X = 3 and Y = 5 . The input for the machine will be #011$101*# . The state in the end should be #1000# .

We can assume X and Y have the same length of bits.

You need to implement a full adder . This question is probably homework, so I will provide a high-level overview. The Turing machine M has special states Q(xyc) where x,y ∈ {0,1,U} and c ∈ {0,1} . The state Q(xyc) means that the i th bit of X is x , the i th bit of Y is y and the carry is c . The symbol U means that the i th bit of the relevant inputs are unknown. The states Q(Uyc) where y ∈ {0,1} are invalid because the i th bit of X is known if the i th bit of Y is known. The algorithm goes something like this:

  1. The initial state of M is Q(UU0) .
  2. Suppose the i th bits of X and Y are being added and the carry is c . Then M is in state Q(UUc) . If i is greater than the number of bits in X and Y , goto step (6). Since the least significant input bits are overwritten in steps (3) and (4), this condition is easy to detect.
  3. Find the least significant bit x of X , overwrite x with $ and transition to state Q(xUc) .
  4. Find the least significant bit y of Y , overwrite y with * and transition to state Q(xyc) .
  5. Write the appropriate sum at the end of the tape, transition to state Q(UUd) where d is the new carry, and goto step (2). These values are given by the truth table in the above link.
  6. If c = 1 , write c at the end of the tape.
  7. Copy the reverse of the computed value to the beginning of the tape. Clear the remaining tape.

Note that the output is constructed in reverse order, and thus must be reversed in step (7). The remaining work consists of writing states and transitions for traversing/manipulating the tape.

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