addition

Java Example to sum of two integer using Bitwise operator

Java Example to sum of two integer using Bitwise operator

In this article, we will discuss the concept of the Java Example to sum of two integers 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 Java programming language

Sum of two integer using Bitwise operator

Code to find the sum of  two numbers 

Sum of two integer using Bitwise operator

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

Program 1

//subtract two integer numbers using bitwise in Java
import java.util.*;
public class SubtractNumBitwise{
public static void main(String args[]){
    int x,y;//variable declaration
    Scanner sc=new Scanner(System.in);
    //scanner class for input
System.out.println("Please Enter the first integer number ");
int num1=sc.nextInt();//ask input from the user and store the input in num1
System.out.println("Please Enter the second integer number ");
int num2=sc.nextInt();//ask input from the user and store the input in num2
x=num1;//Assign the value of num1 to a
y=num2;//Assign the value of num2 to b
while(num2!=0){//Calculate sum of given numbers using bitwise operator
    int brw =num1&num2;
    num1=num1^num2;
    num2=brw<<1;
}
System.out.println("Sum of two numbers "+x+" and "+y+" is: "+num1);
//Display output on the screen
}
}

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

Please Enter the first integer number
250
Please Enter the second integer number
120
Sum of two numbers 250 and 120 is: 370

 

Sum of two integer using Bitwise operator – with Method

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

Program 2

//sum of two integer numbers using bitwise in Java
import java.util.*;
public class SumNumBitwiseMethod{
public static void main(String args[]){
    int x,y;//variable declaration
    Scanner sc=new Scanner(System.in);
    //scanner class for input
System.out.println("Please Enter the first integer number ");
int num1=sc.nextInt();//get input from user for num1
System.out.println("Please Enter the second integer number ");
int num2=sc.nextInt();//get input from user for num1
x=num1;//Assign the value of num1 to x
y=num2;//Assign the value of num2 to y
subNum(num1,num2);//Calling the method with argument
}
static void subNum(int num1,int num2){//method definition with parameter
while(num2!=0){//calculate sum of two numbers using bitwise operator
    int brw =num1&num2;
    num1=num1^num2;
    num2=brw<<1;
    
}
System.out.println("Sum of two numbers is: "+num1);
//display output on the screen

}
}

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

Please Enter the first integer number
1250
Please Enter the second integer number
1500
Sum of two numbers 1250 and 1500 is: 2750

 

Suggested for you

Operator in Java language

Data type and variable in Java language

Class and main method in Java language

If statements in Java language

Method in Java language

 

 

Similar post

Subtract two numbers in Java language

Subtract two numbers using method in Java language

Subtract two numbers using recursion in Java language

 

Find sum of two numbers in Java language

Find sum of two numbers  using method in Java language

Find sum of two numbers in Java using recursion

Find sum of two numbers in Java without Arithmetic operator

Find sum of natural numbers in Java language

Find sum of natural numbers in Java language using recursion

 

C code to subtract 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

PHP Star Triangle pattern program

PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…

2 months ago

PHP Full Pyramid pattern program

PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…

2 months ago

5 methods to add two numbers in Java

5 methods to add two numbers in Java In this tutorial, we will discuss the…

2 months ago

Python Full Pyramid star pattern program

Python full Pyramid star pattern program In this tutorial, we will discuss  the concept of…

5 months ago

Write a function or method to convert C into F -Entered by user

Write a function or method to convert C into F -Entered by the user In…

10 months ago

How to write a function or method to convert Celsius into Fahrenheit

How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…

10 months ago