Ideas
Use a loop to move an integer to the left I bit, and take the highest bit, if the current maximum is 1 then the value is ' 1 ', otherwise ' 0 '. This stitching out a string.
Note: The digital complement is stored in the computer.
About positive, negative number of the original code, anti-code, and complement.
Positive number: The original code, anti-code, and complement are the same.
Negative number: Anti-code (except for the highest bit, other bits to reverse) complement (anti-code + 1)
Why should I store the complement instead of the original code?
In order to unify the operation method. Use the complement to add and subtract signed integers without needing to care about the sign bit.
Example: 1 + (-2) = 1
-2 of the original code
1000 0000 0000 0000 0000 0000 0000 0010
-2 of the anti-code
1111 1111 1111 1111 1111 1111 1111 1101
-2 of the complement
1111 1111 1111 1111 1111 1111 1111 1110
Calculation process:
0000 0000 0000 0000 0000 0000 0000 0001
+ 1111 1111 1111 1111 1111 1111 1111 1110
= 1111 1111 1111 1111 1111 1111 1111 1111
-1 Complement: 1111 1111 1111 1111 1111 1111 1111 1111
The use of complement simplifies the calculation of signed integers into one step, which greatly improves efficiency.
If using the original code: 1. Determine the sign bit of the number. 2. Take the absolute value of the addition/meiosis 3. Plus/minus absolute value
Code
"C + +" gets the binary form string of integers