Prerequisite – Turing Machine, 1’s and 2’s complement of a Binary Number

### Problem-1:

Draw a Turing machine to find 1’s complement of a binary number.

*1’s complement* of a binary number is another binary number obtained by toggling all bits in it, i.e., transforming the 0 bit to 1 and the 1 bit to 0.

**Example:**

**Approach:**

- Scanning input string from left to right
- Converting 1’s into 0’s
- Converting 0’s into 1’s
- Move the head to the start when BLANK is reached.

**Steps:**

**Step-1.**Convert all 0’s into 1’s and all 1’s into 0’s and go right if B found go to left.**Step-2.**Then ignore 0’s and 1’s and go left & if B found go to right**Step-3. Stop the machine.**

Here, **q0** shows the initial state and **q1** shows the transition state and **q2** shows the final state.

And 0, 1 are the variables used and R, L shows right and left.

**Explanation:**

- State q0 replace ‘1’ with ‘0’ and ‘0’ with ‘1’ and move to right.
- When BLANK is reached move towards left.
- Using state ‘q2’ we reach start of the string.
- When BLANK is reached move towards right and reaches the final state q2.

#### Problem-2:

Draw a Turing machine to find 2’s complement of a binary number.

*2’s complement* of a binary number is 1 added to the 1’s complement of the binary number.

**Example:**

**Approach:**

- Scanning input string from right to left
- Pass all consecutive ‘0’s
- For first ‘1’ comes, do nothing
- After that, Converting 1’s into 0’s and Converting 0’s into 1’s
- Stop when BLANK is reached.

**Steps:**

**Step-1.**First ignore all 0’s and 1’s and go to right & then if B found go to left.**Step-2.**Then ignore all 0’s and go left, if 1 found go to left.**Step-3.**Convert all 0’s into 1’s and all 1’s into 0’s and go to left & if B found go to right and**stop the machine.**

Here, **q0** shows the initial state and **q1 and q2 ** shows the transition state and **q3** shows the final state.

And 0, 1 are the variables used and R, L shows right and left.

**Explanation:**

- Using state ‘q0’ we reach end of the string.
- When BLANK is reached move towards left.
- Using state ‘q1’ we passes all 0’s and move left first 1 is found.
- Pass single ‘1’ and move left.
- Using state ‘q2’ we compliment the each digit and move left.
- When BLANK is reached move towards right and reaches the final state q2.

Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.

## Recommended Posts:

- Construct a Turing Machine for language L = {a^n b^m c^nm where n >=0 and m >= 0}
- Difference between Finite Automata and Turing Machine
- Design Turing Machine to reverse String consisting of a's and b's
- Turing Machine for subtraction | Set 2
- Construct Turing machine for L = {a
^{n}b^{m}a^{(n+m)}| n,m≥1} - Construct a Turing Machine for language L = {0
^{n}1^{n}2^{n}| n≥1} - Construct a Turing Machine for language L = {ww
^{r}| w ∈ {0, 1}} - Construct a Turing Machine for language L = {ww | w ∈ {0,1}}
- Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i>j>k; k ≥ 1} - Turing Machine for addition
- Turing machine for multiplication
- Turing machine for subtraction | Set 1
- Turing machine for copying data
- Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i*j = k; i, j, k ≥ 1} - Turing Machine as Comparator
- Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i< j< k; i ≥ 1} - Construct a Turing machine for L = {a
^{i}b^{j}c^{k}| i < j < k or i > j > k} - Construct a Turing Machine for a language L = {a
^{i}b^{j}c^{k}| i<j<k or i>j>k} ∩ {a^{i}b^{j}c^{k}| i>j>k or i>j>k} - Modifications to standard Turing Machine
- Construct a Turing Machine for language L = {0
^{2n}1^{n}| n>=0}

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.