addition

C code to sum of two integer using Bitwise operator

C code to sum two integer using Bitwise operator

In this article, we will discuss the concept of the C code to sum 0f two integer using Bitwise operator

In this post, we are going to learn how to  write a program to find the sum of two numbers using Bitwise operator in C programming language

Sum of two integer using Bitwise operator

Code to find the addition of  two numbers 

Addition of two integer using Bitwise operator

The program allows the user to enter two integers and then calculates sum of given  numbers using  Bitwise operator in C language

Program 1

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int a,b,num1,num2;

    printf("Enter the first number: \n");
//ask input from the user for num1
    scanf("%d",&num1);
//store the input value in num1

     printf("Enter the second number: \n");
//ask input from the user for num2
    scanf("%d",&num2);
//store the input value in num2
    a=num1;//Assign the value of num1 to a
    b=num2;//Assign the value of num2 to b

    while (num2!= 0)
    {//Calculate the sum of two numbers using bitwise operator
        int bwr=num1 & num2;
        num1=num1^num2;
        num2=bwr<<1;
    }
    printf("Sum of %d and %d is: %d",a,b,num1);
//Display output on the screen
getch();
    return 0;
}

When the above code is executed, it produces the following result

Enter the first number:
250
Enter the second number:
350
Sum of 250 and 350 is: 600

 

Addition oft two integer using Bitwise operator – with function

The program allows the user to enter two integers and then calculates sum of given  numbers using  Bitwise operator in C language

Program 2

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1,num2,ans;
    printf("Enter two numbers: \n");
//ask input from the user for num1 and num2
    scanf("%d %d",&num1,&num2);
//store the input values in num1 and num2
    ans=sum(num1,num2);//Calling the function
    printf("Sum of %d and %d is %d",num1,num2,ans);
//Display output on the screen
    getch();
    return 0;
}
int sum(int x, int y){//function definition
  while(y!=0){//Calculate the sum of two numbers using bitwise operator
    int carry=x & y;
    x=x^y;
    y=carry<<1;
  }
  return x;
}

When the above code is executed, it produces the following result

Enter two numbers
300
100
Sum of 300 and 100 is 400


 

Suggested for you

Operator in C language

Data type in C language

variable in C language

if statements in C language

Function in C language

 

 

Similar post

Subtract two numbers in C language

Subtract two numbers using function in C language

Subtract two numbers using recursion in C language

Find sum of two numbers in C language

Find sum of two numbers in C language using pointer

Find sum of two numbers in C language using function

Find sum of two numbers in C using recursion

Find sum of two numbers in C without Arithmetic operator

Find sum of natural numbers in C language

Find sum of natural numbers in C language using recursion

 

Java Example to sum of two integer using Bitwise operator
C++ code to sum of two integer using Bitwise operator
Karmehavannan

I am Mr S.Karmehavannan. Founder and CEO of this website. This website specially designed for the programming learners and very especially programming beginners, this website will gradually lead the learners to develop their programming skill.

Recent Posts

How to find reverse number using method in Java

How to find reverse number using method In this article, we will discuss the concept…

20 hours ago

C# inverted full pyramid star pattern

C# inverted full pyramid star pattern In this article, we will discuss the concept of…

3 weeks ago

C# Full Pyramid star pattern program

C# Full Pyramid star pattern program In this article, we will discuss the concept of…

1 month ago

Program to count vowels,consonants,words, characters and space in Java

Program to count vowels, consonants, words, characters and space in Java In this article, we…

1 month ago

How to print multiplication table using Array in C++ language

How to print multiplication table using Array in C++ language In this post, we will…

1 month ago

C Program to multiplication table using Array

C Program to multiplication table using Array In this tutorial , we will discuss about…

2 months ago