# Design a mealy machine for 2’s complement

Mealy machine is a finite-state machine, its current state and the current inputs determines the output of this machine.

**2’s complement :**

It is the mathematical operation on binary numbers. It is used for computation as a method of signed number representation. Its complement with respect to 2^{N} defines the two’s complement an N-bit number.

**Logic:-**

First calculate 1’s compliment of binary number, convert 1 to 0 and 0 to 1 and then add 1 to it. For example, if binary number is 1011 then its 1’s compliment is 0100 and its 2’s compliment is 0101

**Design mealy machine :**

- Take initial state A.
- If there are n number of zeros at initial state, it will remain at initial state.
- Whenever first input 1 is found then it gives output 1 and go to state B.
- In state B, if input is zero, output will be 1.And if input is 1 then output will be 0.
- And then set state B as final state.

The approach goes as follows:

- Start from right to left.
- Ignore all 0’s.
- When 1 comes ignore it and then take 1’s complement of every digit.

**Figure –**Mealy machine of 2’s compliment

**Example-1:**

- Lets take 001 and we know that its 2’s complement is (110+1 = 111).
- So scan from right to left.
- On state A ‘1’ came first to go to stage B and in output write 1.
- On state B replace ‘0’ with ‘1’ and vice-versa.
- So finally we got 111 as output.
- Be aware that the output is also printed in right to left order.

**Example-2:**

- Lets take 01 and we know that its 2’s complement is (10+1 = 11).
- So scan from right to left.
- On state A ‘1’ came first to go to stage B and in output write 1.
- On state B replace ‘0’ with ‘1’ and vice-versa.
- So finally we got 11 as output.
- Be aware that the output is also printed in right to left order.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Turing Machine in TOC
- Mealy and Moore Machines in TOC
- Turing Machine for subtraction | Set 2
- Construct Turing machine for L = {a
^{n}b^{m}a^{(n+m)}| n,m≥1} - Design 101 sequence detector (Mealy machine)
- 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
- Difference between Mealy machine and Moore machine
- 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 for 1's and 2’s complement
- 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}

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.