logo

Binääriluvun 1:n ja 2:n komplementti

Kun annat binaariluvun merkkijonoksi, tulosta sen 1:n ja 2:n komplementit.

1:n täydennys Binääriluvun on toinen binääriluku, joka saadaan vaihtamalla kaikki siinä olevat bitit, eli muuttamalla 0-bitti 1:ksi ja 1-bitti 0:ksi. 1:n komplementtimuodossa positiiviset luvut pysyvät muuttumattomina. Negatiiviset luvut saadaan ottamalla 1:n komplementti positiivisista vastineista.



esimerkiksi +9 esitetään muodossa 00001001 kahdeksanbittisessä merkinnässä ja -9 esitetään muodossa 11110110, joka on 1:n komplementti luvulle 00001001.

Esimerkkejä:

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

2:n täydennys binääriluvun on 1 lisättynä binääriluvun 1:n komplementtiin. Binäärilukujen 2:n komplementtiesityksessä MSB edustaa etumerkkiä, jossa on '0' plusmerkkiä varten ja '1' miinusmerkkiä varten. jäljellä olevia bittejä käytetään suuruuden esittämiseen. positiiviset magnitudit esitetään samalla tavalla kuin etumerkkibitin tai 1:n komplementtiesityksen tapauksessa. Negatiivisia suuruuksia edustaa niiden positiivisten vastineiden 2:n komplementti.



Esimerkkejä:

kuinka tulostaa java
2's complement of '0111' is '1001' 2's complement of '1100' is '0100'>

Toinen temppu kahden komplementin löytämiseksi:

Vaihe 1: Aloita vähiten merkitsevästä bitistä ja siirry vasemmalle, kunnes löydät luvun 1. Kunnes löydät 1, bitit pysyvät samoina

Vaihe 2: Kun olet löytänyt 1, anna 1 sellaisenaan ja nyt



Vaihe 3: Käännä kaikki jäljellä olevat bitit 1:een.

Kuva

Oletetaan, että meidän on löydettävä 100100:n 2s-komplementti

Vaihe 1: Poikki ja anna bitin pysyä samana, kunnes löydät 1. Tässä x ei ole vielä tiedossa. Vastaus = xxxx00 –

Vaihe 2 : Löysit 1. Anna sen pysyä samana. Vastaus = xxx100

Vaihe 3: Käännä kaikki jäljellä olevat bitit arvoon 1. Vastaus = 011100.

Näin ollen luvun 100100 2s-komplementti on 011100.

Suositeltu harjoituksen 1 täydennys Kokeile!

Täydennyksenä meidän on yksinkertaisesti käännettävä kaikki bitit.
2:n komplementille löydämme ensin toisen komplementin. Kuljemme ykkösen komplementin läpi alkaen LSB:stä (pienin merkitsevä bitti) ja etsimme 0:ta. Käännämme kaikki ykköset (muuta 0:ksi), kunnes löydämme 0:n. Lopuksi käännetään löydetty nolla. Esimerkiksi 2:n komplementti 01000 on 11000 (Huomaa, että löydämme ensin 01000:n komplementin muodossa 10111). Jos kaikki 1:t ovat (yhdessä komplementissa), lisäämme merkkijonoon ylimääräisen 1:n. Esimerkiksi 2:n 000:n komplementti on 1000 (1:n 000:n komplementti on 111).

Alla toteutus.

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 (ykköset[i] == '1') kaksikko[i] = '0'; else { kaksikko[i] = '1'; tauko; } } // Jos Ei taukoa : kaikki ovat 1 kuten 111 tai 11111; // tässä tapauksessa lisää alkuun ylimääräinen 1, jos (i == -1) kaksikko = '1' + kaksi; cout<< '1's complement: ' << ones << endl; cout << '2's complement: ' << twos << endl; } // Driver program int main() { string bin = '1100'; printOneAndTwosComplement(bin); return 0; }>

rakenne tietorakenteessa
>

>

Java




java-luettelosolmu
// 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 { kaksit = kaksit.substring(0, i) + '1' + kaksikko.osamerkkijono(i + 1); tauko; } } // Jos Ei taukoa : kaikki ovat 1 kuten 111 tai 11111; // tässä tapauksessa lisää alkuun ylimääräinen 1 if (i == -1) { kaksikko = '1' + kaksi; } System.out.println('1:n komplementti: ' + ykköset);; System.out.println('2:n komplementti: ' + kaksikko); } // Ohjainkoodi public static void main(String[] args) { String bin = '1100'; tulostaOneAndTwosComplement(bin); } } // Tämän koodin on toimittanut Rajput-Ji>

>

>

Python 3




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


koppi algoritmi



// 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 (ykköset[i] == '1') { kaksikko = kaksikko.Alamerkkijono(0, i) + '0' + kaksikko.Substring(i + 1,twos.Length-( i+1)); } else { kaksit = kaksit.Substring(0, i) + '1' + kaksikko.Alamerkkijono(i + 1,kaksit.Pituus-(i+1)); tauko; } } // Jos Ei taukoa : kaikki ovat 1 kuten 111 tai 11111; // tässä tapauksessa lisää alkuun ylimääräinen 1 if (i == -1) { kaksikko = '1' + kaksi; } Console.WriteLine('1:n komplementti: ' + ykköset);; Console.WriteLine('2:n komplementti: ' + kaksikko); } // Ohjainkoodi public static void Main(String[] args) { String bin = '1100'; tulostaOneAndTwosComplement(bin); } } // Tämän koodin on toimittanut 29AjayKumar>

>

sovelma sovelma

>

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 (ykköset[i] == '1') kaksikko[i] = '0'; else { twos[i] = '1'; tauko; } } kaksikko = kaksikko.join('') // Jos Ei taukoa : kaikki ovat 1 kuten luvuissa 111 tai 11111; // tässä tapauksessa lisää alkuun ylimääräinen 1, jos (i == -1) kaksikko = '1' + kaksi; document.write( '1:n täydennys: ' + ykköset + ' '); document.write( '2:n komplementti: ' + kaksikko + ' '); } // Ohjainohjelma var bin = '1100'; tulostaOneAndTwosComplement(bin);>>

> 

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

Aika monimutkaisuus: Päällä)

Aputila: O(1)