I am a beginner. I am trying to get 2's complement of a binary number which stored in string
sou2_reg='000000000000000000000000000000011'
. If I will do sou2_reg[32]
it gives me 1
, the last bit.
Now what I want to do is getting 2's complement of sou2_reg
. I tried this but its not working. Can you please give me some piece of code?? Really appreciate
string twosComp(string number) {
if ( number == 0 ) { return "1"; }
if ( number == 1 ) { return "0"; }
if ( number % 2 == 0 ) {
return twosComp(number / 2) + "1";
}
else {
return twosComp(number / 2) + "0";
}
}