logo

Complement 1 i 2 d'un nombre binari

Donat un nombre binari com a cadena, imprimiu els seus complements 1 i 2.

complement de 1 d'un nombre binari és un altre nombre binari que s'obté alternant tots els bits que hi ha, és a dir, transformant el bit 0 a 1 i l'1 bit a 0. En el format del complement 1, els nombres positius romanen sense canvis. Els nombres negatius s'obtenen prenent el complement 1 de homòlegs positius.



per exemple, +9 es representarà com 00001001 en notació de vuit bits i -9 es representarà com 11110110, que és el complement 1 de 00001001.

Exemples:

1's complement of '0111' is '1000' 1's complement of '1100' is '0011'>

complement de 2 d'un nombre binari és 1, sumat al complement 1 del nombre binari. En la representació del complement 2 dels nombres binaris, el MSB representa el signe amb un '0' utilitzat per al signe més i un '1' utilitzat per al signe menys. els bits restants s'utilitzen per representar la magnitud. les magnituds positives es representen de la mateixa manera que en el cas de la representació del bit de signe o del complement 1. Les magnituds negatives es representen pel complement de 2 dels seus homòlegs positius.



Exemples:

impressió javascript
2's complement of '0111' is '1001' 2's complement of '1100' is '0100'>

Un altre truc per trobar el complement a dos:

Pas 1: Comenceu des del bit menys significatiu i seguiu cap a l'esquerra fins que trobeu un 1. Fins que trobeu 1, els bits es mantenen iguals.

Pas 2: Un cop hagueu trobat l'1, deixeu que l'1 sigui tal com és, i ara



Pas 3: Gireu tots els bits que queden a l'1.

Il·lustració

Suposem que hem de trobar el complement 2s de 100100

Pas 1: Travessa i deixa que el bit es mantingui igual fins que trobis 1. Aquí x encara no es coneix. Resposta = xxxx00 -

Pas 2 : Has trobat 1. Que segueixi igual. Resposta = xxx100

Pas 3: Inverteix tots els bits que queden a l'1. Resposta = 011100.

Per tant, el complement 2s de 100100 és 011100.

Complement recomanat de la pràctica 1 Prova-ho!

Per a un complement, simplement hem de donar la volta a tots els bits.
Per al complement de 2, primer trobem el complement d'un. Travessem el complement a un començant per LSB (bit menys significatiu) i busquem el 0. Girem tots els 1 (canviem a 0) fins a trobar un 0. Finalment, invertim el 0 trobat. Per exemple, el complement a 2 de 01000 és 11000 (Tingueu en compte que primer trobem el complement de 01000 com a 10111). Si hi ha tots els 1 (en el complement d'un), afegim un 1 addicional a la cadena. Per exemple, el complement a 2 de 000 és 1000 (el complement a 1 de 000 és 111).

A continuació es mostra la implementació.

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--) { if (uns[i] == '1') dos [i] = '0'; else { dos[i] = '1'; trencar; } } // Si No hi ha ruptura: tots són 1 com a 111 o 11111; // en aquest cas, afegiu 1 addicional al principi si (i == -1) dos = '1' + dos; cout<< '1's complement: ' << ones << endl; cout << '2's complement: ' << twos << endl; } // Driver program int main() { string bin = '1100'; printOneAndTwosComplement(bin); return 0; }>

>

mètode principal java
>

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 (uns.charAt(i) == '1') { twos = twos.substring(0, i) + '0' + twos.substring(i + 1); } else { twos = twos.substring(0, i) + '1' + twos.substring(i + 1); trencar; } } // Si No hi ha ruptura: tots són 1 com a 111 o 11111; // en aquest cas, afegiu 1 addicional al principi si (i == -1) { dos = '1' + dos; } System.out.println('1's complement: ' + uns);; System.out.println(complement de '2: ' + dos); } // Codi del controlador public static void main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // Aquest codi aportat per Rajput-Ji>>>

> 

10 de 50




# 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>

>

>

C#


modulació d'amplitud



// 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 (uns[i] == '1') { dos = dos.Subcadena(0, i) + '0' + dos.Subcadena(i + 1,dos.Llargada-( i+1)); } else { twos = twos.Substring(0, i) + '1' + twos.Substring(i + 1,twos.Longitud-(i+1)); trencar; } } // Si No hi ha ruptura: tots són 1 com a 111 o 11111; // en aquest cas, afegiu 1 addicional al principi si (i == -1) { dos = '1' + dos; } Console.WriteLine(complement '1's: ' + uns);; Console.WriteLine(complement de '2: ' + dos); } // Codi del controlador public static void Main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // Aquest codi ha estat aportat per 29AjayKumar>>>

> 

constructor python




> > // 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--) { if (uns[i] == '1') dos [i] = '0'; else { dos[i] = '1'; trencar; } } twos = twos.join('') // Si No hi ha ruptura: tots són 1 com en 111 o 11111; // en aquest cas, afegiu 1 addicional al principi si (i == -1) dos = '1' + dos; document.write( complement de '1: ' + uns + ' '); document.write( complement de '2: ' + dos + ' '); } // Programa del controlador var bin = '1100'; printOneAndTwosComplement(bin); >>>

> 

1's complement: 0011 2's complement: 0100>

Complexitat temporal: O(n)

Espai auxiliar: O(1)