简体   繁体   English

图灵机添加 2 位二进制数

[英]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.嘿,正如标题所说,我正在尝试创建一个添加 2 个 2 位二进制数的图灵机。

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.到目前为止,我设法使它适用于 10 + 01 的情况,但我不能使它适用于所有数字组合。 Could anyone help?有人可以帮忙吗? This is my code so far.到目前为止,这是我的代码。

The input format is X NUM1 NUM2 X NUM3 NUM4 (x10x01):输入格式为 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:您 TM 执行以下操作:

  • State 0: move right until we read a 0, then write 1, move left and go to state 1. State 0:向右移动直到我们读到 0,然后写入 1,向左移动 go 到 state 1。
  • State 1: move left until we read a 1, then write 0, move right and go to state 2. State 1:向左移动,直到我们读到 1,然后写入 0,向右移动,go 到 state 2。
  • State 2: move right until we read a 1, move right and go to state 0. State 2:向右移动,直到我们读到 1,向右移动和 go 到 state 0。
  • State 3 (never reached): Halt State 3(从未达到):暂停

This logic doesn't appear to be adding anything.这个逻辑似乎没有添加任何东西。 On input x10x01 it will:在输入x10x01上,它将:

  1. Tape: x10x01 , state 0, head at position 1 ( x ).磁带: x10x01 ,state 0,头在 position 1 ( x )。 Move right until we read a 0, write 1, move left and go to state 1.向右移动直到我们读取 0,写入 1,向左移动,然后 go 到 state 1。
  2. Tape: x11x01 , state 1, head at position 2 ( 1 ).胶带: x11x01 ,state 1,头在 position 2 ( 1 )。 Move left until we read a 1, then write 0, move right and go to state 2.向左移动直到我们读到 1,然后写入 0,向右移动,然后 go 到 state 2。
  3. Tape: x01x01 , state 2, head at position 3 ( 1 ).胶带: x01x01 ,state 2,头在 position 3 ( 1 )。 Move right until we read a 1, move right and go to state 0.向右移动直到我们读到 1,向右移动和 go 到 state 0。
  4. Tape: x01x01 , state 0, head at position 4 ( x ).磁带: x01x01 ,state 0,头在 position 4 ( x )。
  5. Tape: x01x11 , state 1, head at position 4 ( x ).胶带: x01x11 ,state 1,头在 position 4 ( x )。
  6. Tape: x00x11 , state 2, head at position 4 ( x ).胶带: x00x11 ,state 2,头在 position 4 ( x )。
  7. Tape: x00x11 , state 0, head at position 6 ( 1 ).磁带: x00x11 ,state 0,头在 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.虽然看起来机器添加了0110以获得11 ,但实际上它只是随意地将1 s 和0 s 左右移动。

An actual addition TM would need to do a few more things than your machine is doing.实际添加的 TM 需要做的事情比你的机器做的要多。 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.您也可以对所有可能的输入进行硬编码(只有 16 个),但这可能不符合您分配的作业的精神。

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. State 0:向右移动,直到我们读到一个数字。 Write 0. If the digit is 0, go to state 1. If the digit is 1, go to state 2.写0.如果数字为0,则为go至Z9ED39E2EA2EA931586B6A985A6942EF573EZ1。
  • State 1: move right until we read a digit. State 1:向右移动,直到我们读到一个数字。 If the digit is 0, go to state 3. If the digit is 4, go to state 4.如果数字为0,则为go至Z9ED39E2EA2EA931586B6B6942A6942EF573EZ3。
  • State 2: move right until we read a digit. State 2:向右移动,直到我们读到一个数字。 If the digit is 0, go to state 4. If the digit is 5, go to state 5. If the digit is 0, go to state 4. If the digit is 5, go to state 5.
  • State 3: write 0, halt State 3:写 0,停止
  • State 4: write 1, halt State 4:写 1,停止
  • State 5: write 1, move right go to state 3. State 5:写入 1,向右移动 go 到 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.在此示例中,状态 1 和 2 跟踪第一个数字是 0 还是 1。状态 3、4 和 5 将数字相加。 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). State 3 是当两者都是 0。State 4 是当一个是 1。State 5 是当两者都是 1 时。

The corresponding TM might look like this:对应的 TM 可能如下所示:

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.我将由您来决定如何将这个概念的两次迭代连接在一起以添加两个 2 位数字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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