Given a Binary Number as string, print its 2’s complements.
2’s complement of a binary number is 1 added to the 1’s complement of the binary number. Note that 1’s complement is simply flip of given binary number.
Examples:
2's complement of "0111" is "1001"
2's complement of "1100" is "0100"
We have discussed 1’s and 2’s complements in below post.
1’s and 2’s complement of a Binary Number.
The method discussed in above post traverses binary string twice to find 2’s complement, first finds 1’s complement, then finds 2’s complement using 1’s complement
In this post an efficient method for 2’s complement is discussed that traverses string only once. We traverse the string from last till the single 1 is not traversed and after that flip all values of string i.e. 0 to 1 and 1 to 0.
Note: Here to handle the corner case i.e. if 1 doesn’t exist in the string then just append 1 in the starting of string.
Illustration :
Input: str = "1000100"
Output: 0111100
Explanation: Starts traversing the string from last,
we got first '1' at index 4 then just flip the bits
of 0 to 3 indexes to make the 2's complement.
Input: str = "0000"
Output: 10000
Explanation: As there is no 1 in the string so just
append '1' at starting.
Implementation:
C++
#include<bits/stdc++.h>
using namespace std;
string findTwoscomplement(string str)
{
int n = str.length();
int i;
for (i = n-1 ; i >= 0 ; i--)
if (str[i] == '1' )
break ;
if (i == -1)
return '1' + str;
for ( int k = i-1 ; k >= 0; k--)
{
if (str[k] == '1' )
str[k] = '0' ;
else
str[k] = '1' ;
}
return str;
}
int main()
{
string str = "00000101" ;
cout << findTwoscomplement(str);
return 0;
}
|
Java
class Test
{
static String findTwoscomplement(StringBuffer str)
{
int n = str.length();
int i;
for (i = n- 1 ; i >= 0 ; i--)
if (str.charAt(i) == '1' )
break ;
if (i == - 1 )
return "1" + str;
for ( int k = i- 1 ; k >= 0 ; k--)
{
if (str.charAt(k) == '1' )
str.replace(k, k+ 1 , "0" );
else
str.replace(k, k+ 1 , "1" );
}
return str.toString();
}
public static void main(String[] args)
{
StringBuffer str = new StringBuffer( "00000101" );
System.out.println(findTwoscomplement(str));
}
}
|
Python3
def findTwoscomplement( str ):
n = len ( str )
i = n - 1
while (i > = 0 ):
if ( str [i] = = '1' ):
break
i - = 1
if (i = = - 1 ):
return '1' + str
k = i - 1
while (k > = 0 ):
if ( str [k] = = '1' ):
str = list ( str )
str [k] = '0'
str = ''.join( str )
else :
str = list ( str )
str [k] = '1'
str = ''.join( str )
k - = 1
return str
if __name__ = = '__main__' :
str = "00000101"
print (findTwoscomplement( str ))
|
C#
using System;
using System.Text;
class GFG
{
public static string findTwoscomplement(StringBuilder str)
{
int n = str.Length;
int i;
for (i = n - 1 ; i >= 0 ; i--)
{
if (str[i] == '1' )
{
break ;
}
}
if (i == -1)
{
return "1" + str;
}
for ( int k = i - 1 ; k >= 0; k--)
{
if (str[k] == '1' )
{
str.Remove(k, k + 1 - k).Insert(k, "0" );
}
else
{
str.Remove(k, k + 1 - k).Insert(k, "1" );
}
}
return str.ToString();
}
public static void Main( string [] args)
{
StringBuilder str = new StringBuilder( "00000101" );
Console.WriteLine(findTwoscomplement(str));
}
}
|
PHP
<?php
function findTwoscomplement( $str )
{
$n = strlen ( $str );
$i ;
for ( $i = $n -1 ; $i >= 0 ; $i --)
if ( $str [ $i ] == '1' )
break ;
if ( $i == -1)
return '1' + $str ;
for ( $k = $i -1 ; $k >= 0; $k --)
{
if ( $str [ $k ] == '1' )
$str [ $k ] = '0' ;
else
$str [ $k ] = '1' ;
}
return $str ;;
}
$str = "00000101" ;
echo findTwoscomplement( $str );
?>
|
Javascript
<script>
function findTwoscomplement( str) {
var n = str.length;
var i;
for (i = n - 1; i >= 0; i--)
if (str.charAt(i) == '1' )
break ;
if (i == -1)
return "1" + str;
for (k = i - 1; k >= 0; k--) {
if (str.charAt(k) == '1' )
str = str.substring(0,k)+ "0" +str.substring(k+1, str.length);
else
str = str.substring(0,k)+ "1" +str.substring(k+1, str.length);
}
return str.toString();
}
var str = "00000101" ;
document.write(findTwoscomplement(str));
</script>
|
Time complexity : O(n)
Auxiliary Space : O(1)
If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Jul, 2022
Like Article
Save Article