I’m writing a number converter. How can I convert a integer to a binary string in C# WITHOUT using built-in functions (Convert.ToString does different things based on the value given)?
- Binary -> Sign magnitude
- Binary -> One’s complement
- Binary > Two’s complement
Almost all computers today use two’s complement representation internally, so if you do a straightforward conversion like this, you’ll get the two’s complement string:
That’s your basis for the remaining two conversions. For sign-magnitude, simply extract the sign beforehand and convert the absolute value:
For one’s complement, subtract one if the number is negative: