Dat un număr binar ca șir, imprimați complementele lui 1 și 2.
complementul lui 1 al unui număr binar este un alt număr binar obținut prin comutarea tuturor biților din acesta, adică transformând bitul 0 la 1 și bitul 1 la 0. În formatul complementului 1, numerele pozitive rămân neschimbate. Numerele negative se obțin luând complementul 1 al omologilor pozitivi.
de exemplu, +9 va fi reprezentat ca 00001001 în notație pe opt biți și -9 va fi reprezentat ca 11110110, care este complementul lui 00001001.
Exemple:
1's complement of '0111' is '1000' 1's complement of '1100' is '0011'>
complementul lui 2 al unui număr binar este 1, adăugat la complementul 1 al numărului binar. În reprezentarea complementului 2 a numerelor binare, MSB reprezintă semnul cu un „0” folosit pentru semnul plus și un „1” folosit pentru un semn minus. biții rămași sunt utilizați pentru reprezentarea mărimii. mărimile pozitive sunt reprezentate în același mod ca și în cazul reprezentării biților de semn sau a complementului 1. Mărimile negative sunt reprezentate de complementul 2 al omologilor lor pozitivi.
Exemple:
algoritm de cabină
2's complement of '0111' is '1001' 2's complement of '1100' is '0100'>
Un alt truc pentru a găsi complementul a doi:
Pasul 1: Începeți de la bitul cel mai puțin semnificativ și mergeți spre stânga până când găsiți un 1. Până când găsiți 1, biții rămân aceiași
Pasul 2: Odată ce ați găsit 1, lăsați 1 așa cum este, și acum
Pasul 3: Întoarceți toate biții rămase în 1.
Ilustrare
Să presupunem că trebuie să găsim complementul 2s de 100100
Pasul 1: Traversați și lăsați bitul să rămână același până când găsiți 1. Aici x nu este încă cunoscut. Răspuns = xxxx00 –
Pasul 2 : Ai găsit 1. Lasă-l să rămână la fel. Răspuns = xxx100
Pasul 3: Întoarceți toți biții rămași în 1. Răspuns = 011100.
Prin urmare, complementul 2 al lui 100100 este 011100.
Complementul recomandat pentru practica 1 Încercați!Pentru complement, trebuie pur și simplu să răsturnăm toate bițurile.
Pentru complementul 2, găsim mai întâi complementul unu. Parcurgem complementul unu începând de la LSB (bitul cel mai puțin semnificativ) și căutăm 0. Întoarcem toate cele 1 (schimbați la 0) până găsim un 0. În cele din urmă, întoarcem 0 găsit. De exemplu, complementul 2 de 01000 este 11000 (Rețineți că găsim mai întâi complementul 01000 ca 10111). Dacă există toate 1 (în complementul unu), adăugăm un 1 în plus în șir. De exemplu, complementul 2 de 000 este 1000 (complementul 1 de 000 este 111).
Mai jos este implementarea.
C++
java vs c++
// C++ program to print 1's and 2's complement of> // a binary number> #include> using> namespace> std;> > // Returns '0' for '1' and '1' for '0'> char> flip(>char> c) {>return> (c ==>'0'>)?>'1'>:>'0'>;}> > // Print 1's and 2's complement of binary number> // represented by 'bin'> void> printOneAndTwosComplement(string bin)> {> >int> n = bin.length();> >int> i;> > >string ones, twos;> >ones = twos =>''>;> > >// for ones complement flip every bit> >for> (i = 0; i ones += flip(bin[i]); // for two's complement go from right to left in // ones complement and if we get 1 make, we make // them 0 and keep going left when we get first // 0, make that 1 and go out of loop twos = ones; for (i = n - 1; i>= 0; i--) { dacă (uni[i] == '1') doi[i] = '0'; else { doi[i] = '1'; pauză; } } // Dacă Fără pauză: toate sunt 1 ca în 111 sau 11111; // în acest caz, adăugați 1 suplimentar la început dacă (i == -1) doi = '1' + doi; cout<< '1's complement: ' << ones << endl; cout << '2's complement: ' << twos << endl; } // Driver program int main() { string bin = '1100'; printOneAndTwosComplement(bin); return 0; }> |
>
>
Java
// Java program to print 1's and 2's complement of> // a binary number> > class> GFG> {> > >// Returns '0' for '1' and '1' for '0'> >static> char> flip(>char> c)> >{> >return> (c ==>'0'>) ?>'1'> :>'0'>;> >}> > >// Print 1's and 2's complement of binary number> >// represented by 'bin'> >static> void> printOneAndTwosComplement(String bin)> >{> >int> n = bin.length();> >int> i;> > >String ones =>''>, twos =>''>;> >ones = twos =>''>;> > >// for ones complement flip every bit> >for> (i =>0>; i { ones += flip(bin.charAt(i)); } // for two's complement go from right to left in // ones complement and if we get 1 make, we make // them 0 and keep going left when we get first // 0, make that 1 and go out of loop twos = ones; for (i = n - 1; i>= 0; i--) { if (unii.charAt(i) == '1') { doi = doi.substring(0, i) + '0' + twos.substring(i + 1); } else { doi = doi.substring(0, i) + '1' + doi.substring(i + 1); pauză; } } // Dacă Fără pauză: toate sunt 1 ca în 111 sau 11111; // în acest caz, adăugați 1 suplimentar la început dacă (i == -1) { doi = '1' + doi; } System.out.println(complementul '1's: ' + ones);; System.out.println(complementul '2: ' + doi); } // Cod driver public static void main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // Acest cod a contribuit de Rajput-Ji>>> |
>
ascii de a în java
# Python3 program to print 1's and 2's># complement of a binary number>># Returns '0' for '1' and '1' for '0'>def>flip(c):>>return>'1'>if>(c>=>=>'0'>)>else>'0'>># Print 1's and 2's complement of># binary number represented by 'bin'>def>printOneAndTwosComplement(>bin>):>>>n>=>len>(>bin>)>>ones>=>''>>twos>=>''>>># for ones complement flip every bit>>for>i>in>range>(n):>>ones>+>=>flip(>bin>[i])>>># for two's complement go from right>># to left in ones complement and if>># we get 1 make, we make them 0 and>># keep going left when we get first>># 0, make that 1 and go out of loop>>ones>=>list>(ones.strip(''))>>twos>=>list>(ones)>>for>i>in>range>(n>->1>,>->1>,>->1>):>>>if>(ones[i]>=>=>'1'>):>>twos[i]>=>'0'>>else>:>>twos[i]>=>'1'>>break>>>i>->=>1>># If No break : all are 1 as in 111 or 11111>># in such case, add extra 1 at beginning>>if>(i>=>=>->1>):>>twos.insert(>0>,>'1'>)>>>print>(>'1's complement: '>,>*>ones, sep>=>'')>>print>(>'2's complement: '>,>*>twos, sep>=>'')>># Driver Code>if>__name__>=>=>'__main__'>:>>bin>=>'1100'>>printOneAndTwosComplement(>bin>.strip(''))>># This code is contributed># by SHUBHAMSINGH10>>>string concat javaC#
// C# program to print 1's and 2's complement of>// a binary number>using>System;>>class>GFG>{>>>// Returns '0' for '1' and '1' for '0'>>static>char>flip(>char>c)>>{>>return>(c ==>'0'>) ?>'1'>:>'0'>;>>}>>>// Print 1's and 2's complement of binary number>>// represented by 'bin'>>static>void>printOneAndTwosComplement(String bin)>>{>>int>n = bin.Length;>>int>i;>>>String ones =>''>, twos =>''>;>>ones = twos =>''>;>>>// for ones complement flip every bit>>for>(i = 0; i { ones += flip(bin[i]); } // for two's complement go from right to left in // ones complement and if we get 1 make, we make // them 0 and keep going left when we get first // 0, make that 1 and go out of loop twos = ones; for (i = n - 1; i>= 0; i--) { if (uni[i] == '1') { doi = doi.Substring(0, i) + '0' + doi.Substring(i + 1,doi.Lungime-( i+1)); } else { doi = doi.Substring(0, i) + '1' + doi.Substring(i + 1,doi.Lungime-(i+1)); pauză; } } // Dacă Fără pauză: toate sunt 1 ca în 111 sau 11111; // în acest caz, adăugați 1 suplimentar la început dacă (i == -1) { doi = '1' + doi; } Console.WriteLine(complementul '1's: ' + ones);; Consola.WriteLine(complementul '2: ' + doi); } // Cod driver public static void Main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // Acest cod a fost contribuit de 29AjayKumar>>>>upcasting
>>// Javascript program to print 1's and 2's complement of>// a binary number>>// Returns '0' for '1' and '1' for '0'>function>flip (c) {>return>(c ==>'0'>)?>'1'>:>'0'>;}>>// Print 1's and 2's complement of binary number>// represented by 'bin'>function>printOneAndTwosComplement(bin)>{>>var>n = bin.length;>>var>i;>>>var>ones, twos;>>ones = twos =>''>;>>>// for ones complement flip every bit>>for>(i = 0; i ones += flip(bin[i]); // for two's complement go from right to left in // ones complement and if we get 1 make, we make // them 0 and keep going left when we get first // 0, make that 1 and go out of loop twos = ones; twos = twos.split('') for (i = n - 1; i>= 0; i--) { dacă (uni[i] == '1') doi[i] = '0'; else { doi[i] = '1'; pauză; } } twos = twos.join('') // If No break : toate sunt 1 ca în 111 sau 11111; // în acest caz, adăugați 1 suplimentar la început dacă (i == -1) doi = '1' + doi; document.write( complementul '1: ' + ones + ' '); document.write( complementul '2: ' + doi + ' '); } // Programul driver var bin = '1100'; printOneAndTwosComplement(bin);>>>>1's complement: 0011 2's complement: 0100> Complexitatea timpului: Pe)
Spațiu auxiliar: O(1)