Do school mathematics , compute difference of current digits and carry
int sub = ( ( ( int ) str1 . charAt ( i + diff ) - ( int ) '0' ) - ( ( int ) str2 . charAt ( i ) - ( int ) '0' ) - carry ) ; if ( sub < 0 ) { sub = sub + 10 ; carry = 1 ; } else carry = 0 ; str += String . valueOf ( sub ) ; }