Open In App

Turing machine for 1’s and 2’s complement

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: 



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: 

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

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:

Symbols:

Steps: 

2’s complement 

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: 

Article Tags :