The Wayback Machine - https://web.archive.org/web/20240607083332/https://www.geeksforgeeks.org/turing-machine-for-1s-and-2s-complement/
Open In App

Turing machine for 1’s and 2’s complement

Last Updated : 29 May, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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: 

  1. Scanning input string from left to right
  2. Converting 1’s into 0’s
  3. Converting 0’s into 1’s
  4. 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 is 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: 
 

2’s complement

2’s complement

Approach: 

  1. Start from the end of the input string.
  2. Pass all consecutive ‘0’s.
  3. When you encounter the first ‘1’, do nothing.
  4. After that, replace ‘1’ with ‘0’ and ‘0’ with ‘1’.
  5. Stop when you reach the beginning of the string.

Intuition:

Whenever a numbers 1’s compliment is taken and 1 bit is added to its LSB (Least Significant Bit), all the 1’s (which were originally 0) appearing together from right will add with the carry and change back to 0. Since the first encounter of 0 in 1’s compliment string (which was originally 1) , every ‘1’ bit to its right, if any, will add 1 and change to 0. The first encountered 0 in the string will add 1 and change back to 1, as in the original string. This does not impact any further bit to its left, thus final string have

  1. all 0’s same to the right of 1st ‘1’ bit encountered ( ( complement of 0) + 1) = 0 + carry(=1) to its left element)
  2. the 1st ‘1’ bit encountered will also revert back to 1 the same way.
  3. All other bits to its left will be flipped without any restrictions.

States:

  • q0: Initial state
  • q1, q2: Transition states
  • q3: Final state

Symbols:

  • 0, 1: Binary digits
  • B: Blank symbol
  • R, L: Right and left movements

Steps: 

  • In state q0, move right until you encounter a non-blank symbol. Transition to state q1.
  • In state q1, continue moving right until you encounter the first ‘1’. Transition to state q2.
  • In state q2, replace ‘1’ with ‘0’ and ‘0’ with ‘1’. Move left.
  • When encountering a blank symbol, transition to state q3.
  • In state q3, move right until you reach the end of the string. Halt the machine.

WhatsApp-Image-2024-05-03-at-24251-PM-(1)

  • 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 complement the each digit and move left.
  • When BLANK is reached move towards right and reaches the final state q2.

Previous Article
Next Article

Similar Reads

Difference between Turing machine and Universal Turing machine
Turing Machine was first described by Alan Turing in the year 1936. It was primarily invented to investigate the computability of a given problem. It accepts type-0 grammar which is Recursively Enumerable language. The Turing machine has a tape of infinite length where we can perform read and write operations. The infinite cells of the Turing machi
3 min read
Turing Machine Construction (Transducers Turing Machine) in Java
Prerequisite - Turing Machine Turing Machines can broadly be classified into two types, the Acceptors and the Transducers. Acceptor Turing Machine is an automaton used to define Turing-acceptable languages. Such a machine can be used to check whether a given string belongs to a language or not. It is defined as a 7-tuple machine. Coming to Transduc
10 min read
Designing finite automata for various operations like 1's complement, 2's complement
Transducers in Finite Automata(FA) means FA with Output.There are two types of Machines for FA with Output. 1. Mealy Machine : It is FA in which output is associated with each transition. (that means output depends on state and input).2. Moore Machine : It is FA in which output is associated with each state. (that means output depends on state only
4 min read
Construct a Turing Machine for language L = {a^n b^m c^nm where n >=0 and m >= 0}
Prerequisite - Turing Machine The language L = {anbmcnm | n >= 0 and m>=0} represents a kind of language where we use only 3 symbols, i.e., a, b and c. In the beginning, language has n number of a's followed by m number of b's and then n*m number of c's. Any such string which falls in this category will be accepted by this language. Examples:
3 min read
Difference between Finite Automata and Turing Machine
1. Finite Automata: The finite automata or finite state machine is an abstract machine that has five elements or tuples. It has a set of states and rules for moving from one state to another but it depends upon the applied input symbol. Basically, it is an abstract model of a digital computer. The following figure shows some essential features of g
3 min read
Design Turing Machine to reverse String consisting of a's and b's
Prerequisite : Turing Machine Task : Our task is to design a Turing machine to reverse a string consisting of a's and b's. Examples : Input-1 : aabb Output-1 : bbaa Input-2 : abab Output-2 : baba Approach : The basic idea is to read the input from Right to Left and replace Blank(B) with the alphabet and replace the alphabet with 'X'. When we read a
3 min read
Design a Turing Machine for equal number of a's and b's
Prerequisite - Turing Machine Task : Our task is to design a Turing Machine for an equal number of a's and b's. Analysis : Here the main thing to analyze that string consist of equal numbers of a's and b's can be of 4 types - Here 'n' is the count of a's or b's. a) a^n b^n like aabb b) b^n a^n like bbaa c) (ab)^n like abab d) (ba)^n like baba Examp
3 min read
Construct Turing machine for L = {a<sup>n</sup> b<sup>m</sup> a<sup>(n+m)</sup> | n,m≥1}
L = {an bm a(n+m) | n,m≥1} represents a kind of language where we use only 2 character, i.e., a and b. The first part of language can be any number of "a" (at least 1). The second part be any number of "b" (at least 1). The third part of language is a number of "a" whose count is sum of count of a's in first part of string and count of b's in secon
3 min read
Construct a Turing Machine for language L = {0<sup>n</sup>1<sup>n</sup>2<sup>n</sup> | n≥1}
Prerequisite - Turing Machine The language L = {0n1n2n | n≥1} represents a kind of language where we use only 3 character, i.e., 0, 1 and 2. In the beginning language has some number of 0's followed by equal number of 1's and then followed by equal number of 2's. Any such string which falls in this category will be accepted by this language. The be
3 min read
Construct a Turing Machine for language L = {ww | w &isin; {0,1}}
Prerequisite - Turing Machine The language L = {ww | w ∈ {0, 1}} tells that every string of 0's and 1's which is followed by itself falls under this language. The logic for solving this problem can be divided into 2 parts: Finding the mid point of the string After we have found the mid point we match the symbols Example - Lets understand it with th
7 min read