Če je podano binarno število kot niz, izpišite njegove komplemente 1 in 2.
1 je dopolnilo binarnega števila je drugo binarno število, pridobljeno s preklapljanjem vseh bitov v njem, tj. s pretvorbo bita 0 v 1 in bita 1 v 0. V formatu komplementa 1 ostanejo pozitivna števila nespremenjena. Negativna števila dobimo tako, da vzamemo komplement pozitivnih dvojnikov 1.
na primer +9 bo predstavljeno kot 00001001 v osembitnem zapisu, -9 pa bo predstavljeno kot 11110110, kar je komplement 1 za 00001001.
Primeri:
1's complement of '0111' is '1000' 1's complement of '1100' is '0011'>
2 dopolnilo binarnega števila je 1, dodano k komplementu 1 binarnega števila. V predstavitvi binarnih števil z komplementom 2 MSB predstavlja znak z '0', ki se uporablja za znak plus, in '1', ki se uporablja za znak minus. preostali biti se uporabljajo za predstavitev velikosti. pozitivne magnitude so predstavljene na enak način kot v primeru predznakovnega bita ali komplementarne predstavitve 1. Negativne magnitude so predstavljene s komplementom 2 svojih pozitivnih dvojnikov.
Primeri:
pomlad mvc
2's complement of '0111' is '1001' 2's complement of '1100' is '0100'>
Še en trik za iskanje komplementa dveh:
Korak 1: Začnite od najmanj pomembnega bita in se premikajte levo, dokler ne najdete 1. Dokler ne najdete 1, ostanejo bitovi enaki
2. korak: Ko ste našli 1, pustite 1, kot je, in zdaj
3. korak: Obrnite vse preostale dele v 1.
Ilustracija
Recimo, da moramo najti 2s komplement od 100100
Korak 1: Premikajte in pustite bit enak, dokler ne najdete 1. Tukaj x še ni znan. Odgovor = xxxx00 –
2. korak : Našli ste 1. Naj ostane tako. Odgovor = xxx100
3. korak: Obrnite vse preostale bite v 1. Odgovor = 011100.
Zato je komplement 2s od 100100 011100.
Dopolnitev priporočene prakse 1. Poskusite!Za svoje dopolnilo moramo preprosto obrniti vse bite.
Za komplement 2 najprej poiščemo komplement ena. Prečkamo komplement enice, začenši z LSB (najmanj pomemben bit), in iščemo 0. Obračamo vse 1 (spremenimo v 0), dokler ne najdemo 0. Na koncu obrnemo najdeno 0. Na primer, komplement 2 za 01000 je 11000 (Upoštevajte, da najprej najdemo svoj komplement 01000 kot 10111). Če so vse 1 (v enem komplementu), v nizu dodamo dodatno 1. Na primer, komplement 2 za 000 je 1000 (komplement 1 za 000 je 111).
Spodaj je izvedba.
C++
java zamenja ves niz
// 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 (ones[i] == '1') twos[i] = '0'; else { twos[i] = '1'; odmor; } } // Če ni prekinitve: vsi so 1 kot v 111 ali 11111; // v takem primeru dodajte dodatno 1 na začetku if (i == -1) twos = '1' + twos; cout<< '1's complement: ' << ones << endl; cout << '2's complement: ' << twos << endl; } // Driver program int main() { string bin = '1100'; printOneAndTwosComplement(bin); return 0; }> |
>
>
Java
alya manasa
// 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 (ones.charAt(i) == '1') { twos = twos.substring(0, i) + '0' + twos.substring(i + 1); } else { twos = twos.substring(0, i) + '1' + twos.substring(i + 1); odmor; } } // Če ni prekinitve: vsi so 1 kot v 111 ali 11111; // v takem primeru dodajte dodatno 1 na začetku if (i == -1) { twos = '1' + twos; } System.out.println(komplement '1: ' + one);; System.out.println('2's komplement: ' + dvojke); } // Koda gonilnika public static void main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // To kodo je prispeval Rajput-Ji> |
>
>
Python3
# 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#
// 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 (enice[i] == '1') { twos = twos.Substring(0, i) + '0' + twos.Substring(i + 1,twos.Length-( i+1)); } else { twos = twos.Substring(0, i) + '1' + twos.Substring(i + 1,twos.Length-(i+1)); odmor; } } // Če ni prekinitve: vsi so 1 kot v 111 ali 11111; // v takem primeru dodajte dodatno 1 na začetku if (i == -1) { twos = '1' + twos; } Console.WriteLine(komplement '1: ' + enice);; Console.WriteLine('2's komplement: ' + dvojke); } // Koda gonilnika public static void Main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // To kodo je prispeval 29AjayKumar> |
c naključno število
>
java razvrščanje nizov
>
Javascript
> > // 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 (ones[i] == '1') twos[i] = '0'; else { twos[i] = '1'; odmor; } } twos = twos.join('') // Če ni prekinitve: vsi so 1 kot v 111 ali 11111; // v takem primeru dodajte dodatno 1 na začetku if (i == -1) twos = '1' + twos; document.write( '1's komplement: ' + enice + ' '); document.write( '2's komplement: ' + dvojke + ' '); } // Program gonilnika var bin = '1100'; printOneAndTwosComplement(bin);> |
>
>
Izhod:
1's complement: 0011 2's complement: 0100>
Časovna zapletenost: O(n)
Pomožni prostor: O(1)