I have a binary string as follows:
String bin = \"1101\";
and I want my int\'s value to be -3 instead of 13
-3
13
Use if (i >= k) i -= 2 * k; Where k is the smallest positive number your scheme cannot represent. (8 in this case, because 0111 is 7 and 1000 would be negative.)
if (i >= k) i -= 2 * k;
k
0111
1000
13 is greater than 8, so you'd subtract 16 from 13, giving -3.
8
16