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
' NEW_LINE def flip ( c ) : NEW_LINE INDENT return '1' if ( c == '0' ) else '0' NEW_LINE DEDENT def printOneAndTwosComplement ( bin ) : NEW_LINE INDENT n = len ( bin ) NEW_LINE ones = " " NEW_LINE twos = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT ones += flip ( bin [ i ] ) NEW_LINE DEDENT ones = list ( ones . strip ( " " ) ) NEW_LINE twos = list ( ones ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( ones [ i ] == '1' ) : NEW_LINE INDENT twos [ i ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT twos [ i ] = '1' NEW_LINE break NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE if ( i == - 1 ) : NEW_LINE INDENT twos . insert ( 0 , '1' ) NEW_LINE DEDENT print ( "1 ' s ▁ complement : ▁ " , * ones , sep = " " ) NEW_LINE print ( "2 ' s ▁ complement : ▁ " , * twos , sep = " " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT bin = "1100" NEW_LINE printOneAndTwosComplement ( bin . strip ( " " ) ) NEW_LINE DEDENT