Turing machine for 1's and 2’s complement Last Updated : 01 Aug, 2024 Comments Improve Suggest changes 10 Likes Like Report 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:1's ComplementApproach:Scanning input string from left to rightConverting 1's into 0'sConverting 0's into 1'sStop when you reach the end of the string (when BLANK is encountered)States:q0: Initial stateqH: Final stateSymbols:0, 1: Binary digitsB: Blank symbolR, L: Right and left movementsSteps: Step-1. Convert all 0's into 1's and all 1's into 0's and if B is found go to right. Step-2. Stop the machine.Turing Machine:Turing Machine for 1's ComplementExplanation: State q0 replaces '1' with '0' and '0' with '1' and moves to the right.When BLANK is reached, move towards the right and reach the final state qH. 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 ComplementApproach: Start from the end of the input string.Pass all consecutive '0's.When you encounter the first '1', do nothing.After that, replace '1' with '0' and '0' with '1'.Stop when you reach the beginning of the string.Intuition:Whenever a number's 1's complement 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 complement 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 All 0's same to the right of first '1' bit encountered ( ( complement of 0) + 1) = 0 + carry(=1) to its left element)The first '1' bit encountered will also revert back to 1 the same way. All other bits to its left will be flipped without any restrictions.States:q0: Initial stateq1, q2: Transition statesqH: Final stateSymbols:0, 1: Binary digitsB: Blank symbolR, L: Right and left movementsSteps: In state q0, move right until you encounter a BLANK symbol. Then, transition to state q1 and go to left.In state q1, continue moving left until you encounter the first '1'. Transition to state q2.In state q1, if BLANK symbol is encountered, then transition to state qH.In state q2, replace '1' with '0' and '0' with '1'.If BLANK symbol is encountered, then transition to state qH.In state qH, halt the machine.Turing MachineTuring Machine for 2's ComplementExplanation:Using state 'q0' we reach the end of the string.When BLANK is reached, move towards the left.Using state 'q1' we pass all 0's and move left till first '1' is found.If before finding '1' we encounter BLANK, it implies the number is 0 i.e. it contains no 1.Hence its 2s complement is 0 itself. So no need to change any symbols and just halt the machine.If '1' is encountered, skip that '1' and move left.Using state 'q2' we complement the each digit and move left.When BLANK is reached, move towards the right and reach the final state qH. Create Quiz Comment S shubhamsingh10 Follow 10 Improve S shubhamsingh10 Follow 10 Improve Article Tags : GATE CS Theory of Computation Explore Automata _ IntroductionIntroduction to Theory of Computation5 min readChomsky Hierarchy in Theory of Computation2 min readApplications of various Automata4 min readRegular Expression and Finite AutomataIntroduction of Finite Automata3 min readArden's Theorem in Theory of Computation6 min readSolving Automata Using Arden's Theorem6 min readL-graphs and what they represent in TOC4 min readHypothesis (language regularity) and algorithm (L-graph to NFA) in TOC7 min readRegular Expressions, Regular Grammar and Regular Languages7 min readHow to identify if a language is regular or not8 min readDesigning Finite Automata from Regular Expression (Set 1)4 min readStar Height of Regular Expression and Regular Language3 min readGenerating regular expression from Finite Automata3 min readCode Implementation of Deterministic Finite Automata (Set 1)8 min readProgram for Deterministic Finite Automata7 min readDFA for Strings not ending with "THE"12 min readDFA of a string with at least two 0âs and at least two 1âs3 min readDFA for accepting the language L = { anbm | n+m =even }14 min readDFA machines accepting odd number of 0âs or/and even number of 1âs3 min readDFA of a string in which 2nd symbol from RHS is 'a'10 min readUnion Process in DFA4 min readConcatenation Process in DFA3 min readDFA in LEX code which accepts even number of zeros and even number of ones6 min readConversion from NFA to DFA5 min readMinimization of DFA7 min readReversing Deterministic Finite Automata4 min readComplementation process in DFA2 min readKleene's Theorem in TOC | Part-13 min readMealy and Moore Machines in TOC3 min readDifference Between Mealy Machine and Moore Machine4 min readCFGRelationship between grammar and language in Theory of Computation4 min readSimplifying Context Free Grammars6 min readClosure Properties of Context Free Languages11 min readUnion and Intersection of Regular languages with CFL3 min readConverting Context Free Grammar to Chomsky Normal Form5 min readConverting Context Free Grammar to Greibach Normal Form6 min readPumping Lemma in Theory of Computation4 min readCheck if the language is Context Free or Not4 min readAmbiguity in Context free Grammar and Languages3 min readOperator grammar and precedence parser in TOC6 min readContext-sensitive Grammar (CSG) and Language (CSL)2 min readPDA (Pushdown Automata)Introduction of Pushdown Automata5 min readPushdown Automata Acceptance by Final State4 min readConstruct Pushdown Automata for given languages4 min readConstruct Pushdown Automata for all length palindrome6 min readDetailed Study of PushDown Automata3 min readNPDA for accepting the language L = {anbm cn | m,n>=1}2 min readNPDA for accepting the language L = {an bn cm | m,n>=1}2 min readNPDA for accepting the language L = {anbn | n>=1}2 min readNPDA for accepting the language L = {amb2m| m>=1}2 min readNPDA for accepting the language L = {am bn cp dq | m+n=p+q ; m,n,p,q>=1}2 min readConstruct Pushdown automata for L = {0n1m2m3n | m,n ⥠0}3 min readConstruct Pushdown automata for L = {0n1m2n+m | m, n ⥠0}2 min readNPDA for accepting the language L = {ambncm+n | m,n ⥠1}2 min readNPDA for accepting the language L = {amb(m+n)cn| m,n ⥠1}3 min readNPDA for accepting the language L = {a2mb3m|m>=1}2 min readNPDA for accepting the language L = {amb2m+1 | m ⥠1}2 min readNPDA for accepting the language L = {aibjckdl | i==k or j==l,i>=1,j>=1}3 min readConstruct Pushdown automata for L = {a2mc4ndnbm | m,n ⥠0}3 min readNPDA for L = {0i1j2k | i==j or j==k ; i , j , k >= 1}2 min readNPDA for accepting the language L = {anb2n| n>=1} U {anbn| n>=1}2 min readNPDA for the language L ={wÐ{a,b}* | w contains equal no. of a's and b's}3 min readTuring MachineTuring Machine in TOC7 min readTuring Machine for addition3 min readTuring machine for subtraction | Set 12 min readTuring machine for multiplication2 min readTuring machine for copying data2 min readConstruct a Turing Machine for language L = {0n1n2n | nâ¥1}3 min readConstruct a Turing Machine for language L = {wwr | w ∈ {0, 1}}5 min readConstruct a Turing Machine for language L = {ww | w ∈ {0,1}}7 min readConstruct Turing machine for L = {an bm a(n+m) | n,mâ¥1}3 min readConstruct a Turing machine for L = {aibjck | i*j = k; i, j, k ⥠1}2 min readTuring machine for 1's and 2âs complement3 min readRecursive and Recursive Enumerable Languages in TOC6 min readTuring Machine for subtraction | Set 22 min readHalting Problem in Theory of Computation4 min readTuring Machine as Comparator3 min readDecidabilityDecidable and Undecidable Problems in Theory of Computation6 min readUndecidability and Reducibility in TOC5 min readComputable and non-computable problems in TOC6 min readTOC Interview preparationLast Minute Notes - Theory of Computation13 min readTOC Quiz and PYQ's in TOCTheory of Computation - GATE CSE Previous Year Questions2 min read Like