Home » C solved programs » Advance C programs

**Binary addition/subtraction** is similar to regular (daily life) addition/subtraction, but here **addition/subtraction** performs only two digits those are **0 and 1**, these are binary digits hence such kind of addition/subtraction is called **binary addition/subtraction**.

**Example of Binary Addition:**

Take two numbers, suppose numbers are **10 and 20** their binaries are **1010 and 10100**.

In binary addition 0+0 = 0 0+1 = 1 1+0 = 1 1+1 = 0 (1 carries out) Now add 10 and 20 10 = 0 1 0 1 0 20 = 1 0 1 0 0 = 1 1 1 1 0 = 30

**Example of Binary Subtraction:**

Take two numbers, suppose numbers are **20 and 10** their binaries **10100 and 1010**.

In binary subtraction 0-0 = 0 1-0 = 1 0-1 = 1 (1 borrows out) 1-1 = 0 Now sub 10 from 20 20 = 1 0 1 0 0 10 = 0 1 0 1 0 = 0 1 0 1 0 = 10

*Consider the example:*

#include <stdio.h> //function for Binary Addition int binAddition(int a,int b) { int c; //carry while (b != 0) { //find carry and shift it left c = (a & b) << 1; //find the sum a=a^b; b=c; } return a; } //function for Binary Subtraction int binSubtracton(int a, int b) { int carry; //get 2's compliment of b and add in a b = binAddition(~b, 1); while (b != 0) { //find carry and shift it left carry = (a & b) << 1; //find the sum a = a ^ b; b = carry; } return a; } int main() { int number1,number2, binAdd, binSub; printf("Input first integer value: "); scanf("%d",&number1); printf("Input second integer value: "); scanf("%d",&number2); binAdd=binAddition(number1,number2); binSub=binSubtracton(number1,number2); printf("Binary Addition: %d\n",binAdd); printf("Binary Subtraction: %d\n",binSub); return 0; }

**Output**

Input first integer value: 30 Input second integer value: 5 Binary Addition: 35 Binary Subtraction: 25

Are you a blogger? Join our Blogging forum.

Comments and Discussions

.resCodeAS1 { display: block; width: 320px; height: 50px; }
@media(min-width: 300px) { .resCodeAS1 { display: none; } }
@media(min-width: 480px) { .resCodeAS1 { display: none; } }
@media(min-width: 750px) { .resCodeAS1 { display: block; width: 336px; height: 280px; } }
(adsbygoogle = window.adsbygoogle || []).push({});

(adsbygoogle = window.adsbygoogle || []).push({});

(adsbygoogle = window.adsbygoogle || []).push({});

solved programs: » C » C++ » DS » Java » C# |

aptitude que. & ans.: » C » C++ » Java » DBMS |

interview que. & ans.: » C » Embedded C » Java » SEO » HR |

Like other websites, this site uses cookies to deliver relevant ads based on your interest, by using our website, you acknowledge that you have read our privacy policy.

Close