Addition of two binary numbers using C

Basic Theory

Addition of binary numbers is far simpler than that of a decimal number. This is because binary addition includes addition between 1 and 0 only. The addition process results into two units: sum and carry
sum = a xor b xor c
carry = ab + bc+ ac

Initially, the carry bit is set to be 0. This process is continued until all the bits in a binary number finish.

Code Assumption

The following code uses 8 – bit binary unsigned integer. This means each integer can hold from 0 to 255 in decimal. So user can add up to 255. If the result is greater than 255, then the condition of overflow occurs. The code contains the following segments
  • Decimal to Binary Conversion
  • Addition of two binary numbers
  • Binary to Decimal conversion
This is, the first user enters the operands in decimal format like 100 and 125. Then program converts both operands into an equivalent binary format like 011001000 and 01111101. The program adds two binary numbers using the above method. The results are obtained in binary format like 11100001. Then finally the binary number is converted back to decimal format like 225.
Source Code:
SHARE Addition of two binary numbers using C

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Share