给定一个二进制数作为字符串,打印其1和2的补码。
1的补语 一个二进制数的整数是另一个二进制数,通过切换其中的所有位来获得,即将0位转换为1,将1位转换为0。
例如:
1's complement of "0111" is "1000"1's complement of "1100" is "0011"
2的补语 一个二进制数的整数是1,加在二进制数的1的补码上。
例如:
2's complement of "0111" is "1001"2's complement of "1100" is "0100"
找到二的补码的另一个技巧:
第一步: 从最低有效位开始向左遍历,直到找到1。在找到1之前,位保持不变
第二步: 一旦你找到了1,让1保持原样,现在
第三步: 将所有剩余的位翻转到1中。
插图
假设我们需要找到100100的2s补码
第一步: 遍历并让钻头保持不变,直到找到1。这里x还不知道。答案=xx00-
第二步 :你找到了1个。让它保持不变。答案=xxx100
第三步: 将所有剩余的位翻转到1中。答案=011100。
因此,100100的2s补码是011100。
作为补语,我们只需要翻转所有位。 对于2的补语,我们首先找到1的补语。我们从LSB(最低有效位)开始遍历1的补码,并查找0。我们翻转所有1(变为0),直到找到一个0。最后,我们翻转找到的0。例如,2的“01000”补码是“11000”(注意,我们首先发现1的01000补码是10111)。如果有所有的1(在补码中),我们在字符串中添加一个额外的1。例如,2的“000”补码是“1000”(1的“000”补码是“111”)。
下面是实现。
C++
// C++ program to print 1's and 2's complement of // a binary number #include <bits/stdc++.h> 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 < n; 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' ; break ; } } // If No break : all are 1 as in 111 or 11111; // in such case, add extra 1 at beginning 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
// 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 < n; 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 ); break ; } } // If No break : all are 1 as in 111 or 11111; // in such case, add extra 1 at beginning if (i == - 1 ) { twos = '1' + twos; } System.out.println( "1's complement: " + ones);; System.out.println( "2's complement: " + twos); } // Driver code public static void main(String[] args) { String bin = "1100" ; printOneAndTwosComplement(bin); } } // This code contributed by 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 < n; 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)); break ; } } // If No break : all are 1 as in 111 or 11111; // in such case, add extra 1 at beginning if (i == -1) { twos = '1' + twos; } Console.WriteLine( "1's complement: " + ones);; Console.WriteLine( "2's complement: " + twos); } // Driver code public static void Main(String[] args) { String bin = "1100" ; printOneAndTwosComplement(bin); } } // This code has been contributed by 29AjayKumar |
Javascript
<script> // 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 < n; 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 '; break; } } twos = twos.join(' ') // If No break : all are 1 as in 111 or 11111; // in such case, add extra 1 at beginning if (i == -1) twos = ' 1 ' + twos; document.write( "1' s complement: " + ones + "<br>" ); document.write( "2's complement: " + twos + "<br>" ); } // Driver program var bin = "1100" ; printOneAndTwosComplement(bin); </script> |
输出:
1's complement: 00112's complement: 0100
时间复杂性: O(n)
辅助空间: O(1)
幸亏 乌特卡什·特里维迪 对于上述解决方案。 作为旁注,有符号的数字通常使用2的补码表示。正值按原样存储,负值以2的补码形式存储。需要一个额外的位来指示数字是正还是负。例如,字符在C中为8位。如果字符使用2的补码表示,则127按原样存储,即01111111,其中前0表示正。但是-127存储为10000001。 相关帖子: 二进制字符串2的补码的一种有效方法 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。 参考资料: http://qa.geeksforgeeks.org/6439/write-program-calculate-ones-and-twos-complement-of-number http://geeksquiz.com/whats-difference-between-1s-complement-and-2s-complement/