logo

이진수의 1과 2의 보수

이진수가 문자열로 주어지면 1과 2의 보수를 인쇄합니다.

1의 보수 이진수는 그 안의 모든 비트를 토글하여 얻은 또 다른 이진수입니다. 즉, 0비트를 1로, 1비트를 0으로 변환합니다. 1의 보수 형식에서 양수는 변경되지 않습니다. 음수는 양수 대응의 1의 보수를 취하여 얻습니다.



예를 들어 +9는 8비트 표기법에서 00001001로 표시되고 -9는 00001001의 1의 보수인 11110110으로 표시됩니다.

예:

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

2의 보수 이진수의 1은 이진수의 1의 보수에 더해집니다. 이진수의 2의 보수 표현에서 MSB는 더하기 기호에 '0'을 사용하고 빼기 기호에 '1'을 사용하여 기호를 나타냅니다. 나머지 비트는 크기를 나타내는 데 사용됩니다. 양의 크기는 부호비트나 1의 보수 표현의 경우와 같은 방식으로 표현됩니다. 음의 크기는 양의 크기에 대한 2의 보수로 표시됩니다.



예:

직원 선발 커미션의 의미
2's complement of '0111' is '1001' 2's complement of '1100' is '0100'>

2의 보수를 찾는 또 다른 요령:

1 단계: 최하위 비트부터 시작하여 1을 찾을 때까지 왼쪽으로 이동합니다. 1을 찾을 때까지 비트는 동일하게 유지됩니다.

2 단계: 1을 찾았으면 1을 그대로 두고 이제



3단계: 남은 비트를 모두 1로 뒤집습니다.

삽화

100100의 2s 보수를 찾아야 한다고 가정해 보겠습니다.

1 단계: 탐색하여 1을 찾을 때까지 비트를 동일하게 유지합니다. 여기서 x는 아직 알려지지 않았습니다. 답 = xxxx00 –

2 단계 : 1개를 찾았습니다. 그대로 유지하세요. 답 = xxx100

3단계: 남은 모든 비트를 1로 뒤집습니다. 답 = 011100.

따라서 100100의 2의 보수는 011100이 됩니다.

추천 연습 1의 보완 실습해 보세요!

보완을 위해서는 모든 비트를 뒤집기만 하면 됩니다.
2의 보수의 경우 먼저 1의 보수를 찾습니다. LSB(최하위 비트)부터 시작하여 1의 보수를 탐색하고 0을 찾습니다. 0을 찾을 때까지 모든 1을 뒤집습니다(0으로 변경). 마지막으로 찾은 0을 뒤집습니다. 예를 들어 01000의 2의 보수는 다음과 같습니다. 11000(먼저 01000의 보수를 10111로 찾는다는 점에 유의하세요). (1의 보수에) 모두 1이 있으면 문자열에 1을 추가합니다. 예를 들어, 000의 2의 보수는 1000입니다(000의 1의 보수는 111입니다).

아래는 구현입니다.

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 (ones[i] == '1') twos[i] = '0'; else { twos[i] = '1'; 부서지다; } } // 중단이 없는 경우: 111 또는 11111과 같이 모두 1입니다. // 이 경우 처음에 1을 추가합니다. if (i == -1) twos = '1' + twos; 시합<< '1's complement: ' << ones << endl; cout << '2's complement: ' << twos << endl; } // Driver program int main() { string bin = '1100'; printOneAndTwosComplement(bin); return 0; }>

>

>

자바




// 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); 부서지다; } } // 중단이 없는 경우: 111 또는 11111과 같이 모두 1입니다. // 이 경우 처음에 1을 추가합니다. if (i == -1) { twos = '1' + twos; } System.out.println('1의 보수: ' + 1);; System.out.println('2's 보수: ' + 2); } // 드라이버 코드 public static void main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // 이 코드는 Rajput-Ji가 제공한 것입니다.>

>

>

파이썬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# 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 (ones[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)); 부서지다; } } // 중단이 없는 경우: 111 또는 11111과 같이 모두 1입니다. // 이 경우 처음에 1을 추가합니다. if (i == -1) { twos = '1' + twos; } Console.WriteLine('1's 보수: ' + 1);; Console.WriteLine('2's 보수: ' + 2); } // 드라이버 코드 public static void Main(String[] args) { String bin = '1100'; printOneAndTwosComplement(bin); } } // 이 코드는 29AjayKumar가 기여했습니다.>

>

치란지비 배우

>

자바스크립트




> > // 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'; 부서지다; } } twos = twos.join('') // 중단이 없는 경우: 111 또는 11111에서와 같이 모두 1입니다. // 이 경우 처음에 1을 추가합니다. if (i == -1) twos = '1' + twos; document.write( '1의 보수: ' + 1 + ' '); document.write( '2의 보수: ' + 2 + ' '); } // 드라이버 프로그램 var bin = '1100'; printOneAndTwosComplement(bin);>

>

>

산출:

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

시간 복잡도: 에)

보조 공간: 오(1)