A 2’s complement is used for binary operations in a computer system. In this example program, you are going to receive a binary string and convert the string into 2’s complement in two steps.
Learn the basics of C programming before you begin with the example. Skip this step if you already familiar with these concepts.
- C Program Structure.
- How to install Turbo C++ 3.2 Compiler.
- C Data Types.
- C Flow Control Structures.
- C For Loop.
- C Functions.
The program ask for a input binary string. To convert this binary string in to 2’s complement, you need two steps.
- Convert the input binary string to 1’s complement.
- Add 1 to the 1’s complement obtained.
For example, suppose a the input string is 1100.
Step 1: The 1’s complement can be obtained by converting 1’s in to 0 and 0s into 1s.
1100 becomes 0011
Step 2: Add 1 to the 1’s complement to get 2’s complement of the number.
This step requires you to understand the binary math.