1 ' s ▁ and ▁ 2' s complement of a Binary Number | Java program to print 1 ' s ▁ and ▁ 2' s complement of a binary number ; Returns '0' for '1' and '1' for '0' ; Print 1 ' s ▁ and ▁ 2' s complement of binary number represented by " bin " ; for ones complement flip every bit ; 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 ; If No break : all are 1 as in 111 or 11111 ; in such case , add extra 1 at beginning ; Driver code
class GFG { static char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } static void printOneAndTwosComplement ( String bin ) { int n = bin . length ( ) ; int i ; String ones = " " , twos = " " ; ones = twos = " " ; for ( i = 0 ; i < n ; i ++ ) { ones += flip ( bin . charAt ( i ) ) ; } 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 ( i == - 1 ) { twos = '1' + twos ; } System . out . println ( "1 ' s ▁ complement : ▁ " + ones ) ; ; System . out . println ( "2 ' s ▁ complement : ▁ " + twos ) ; } public static void main ( String [ ] args ) { String bin = "1100" ; printOneAndTwosComplement ( bin ) ; } }