Question: adding two numbers using bitwise operators

adding two numbers using bitwise operators
3 Answer(s)      6 years and 4 months ago
Posted in : Java Interview Questions

adding two integer numbers with using bitwise opeators

January 5, 2011 at 11:31 AM

Hi Friend,

Try the following code:

```public class AddNumberUsingBitwiseOperators {
public static void main(String[] args) {
int x=5,y=6;
int xor, and, temp;
and = x & y;
xor = x ^ y;

while(and != 0 )
{
and <<= 1;
temp = xor ^ and;
and &= xor;
xor = temp;
}
System.out.println(xor);
}
}
```

Thanks

October 31, 2011 at 6:54 PM

*hi friends , my name is Rajasekhar kadiveti "43857" the above program has one error,so remove that line i.e is*

and &=xor; the above can be written in simple format .

Logic:

```int X,Y;
int Z=(((X&Y)<<1)^(X^Y));
System.out.println(Z);
```

OR

```class Sum
{
public static viod main(String args[])
{
int X,Y,A,B;

X=3;

y=4;

A=X&Y;

B=X^Y;

A=A<<1;

int Z=A^B;

System.out.println("sum="+Z);
```

}

}

out put: step by step

X=0000 0011

Y=0000 0100

A=0000 0000 (X&Y)

B=0000 0111 (X^Y)

A=0000 0000 (A<<1)

Z=0000 0111 (A^B)

Z=7.

June 5, 2012 at 10:02 AM

Eg:

x = 10, y = 20, z = 0; z = ( (x&y) << 1 ) | (x^y);

adding two numbers using bitwise operators
adding two numbers using bitwise operators  adding two integer numbers with using bitwise opeators   Hi Friend, Try the following code...[] args) { System.out.println("Adding 5 and 6......"); int x=5,y=6; int xor
adding two numbers with out using any operator
adding two numbers with out using any operator  how to add two numbers with out using any operator   import java.math.*; class AddNumbers { public static void main(String[] args) { BigInteger num1=new
Adding two numbers  Accepting value ffrom the keyboard and adding two numbers
bitwise operators
bitwise operators  Write short note on bitwise operators of Java.   Please visit the following links: http://www.roseindia.net/java/java-tips/data/expressions/slides-bitops.shtml http://www.roseindia.net/java/java
adding of two numbers in designing of frame
adding of two numbers in designing of frame  hello sir, now i'm create two textfield for mark1&mark2 from db.how to add these two numbers in another one text field.how to write a coding... if u have another one idea pls
adding two numbers - Java Beginners
adding two numbers  hii friends....... this is my program...]=Integer.parseInt(in.readLine()); int s=x[1]+y[1]; System.out.println("Sum of two...]; System.out.println("Sum of two no. is:"+s); } } For read more
bitwise operators
bitwise operators  include<stdio.h> int main(){ short x... this program give the answer as 7  hi friend, Bitwise OR (|) operator returns 1 if either side of operand is 1 else it returns 0 i.e. in this case the bitwise
Bitwise Operators in java 7
In this section you will learn about the Bitwise Operators. This is one type of operators
bitwise operators accept type char as operands
bitwise operators accept type char as operands   Why bitwise operators accept type char as operands
Bitwise and Bit Shift Operators
Bitwise and Bit Shift Operators       In Java the bitwise and bit shift operators are used.... These operators perform bitwise and bit shift operations on integral type
Sum of two numbers using command line arguments in Java
Sum of two numbers using command line arguments in Java  Hi, How...? The calculate the sum of two numbers using command line arguments in Java... sum = a+b; Check complete example code at Add Two Numbers in Java. Thanks
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
To find first two maximum numbers in an array,using single loop without sorting array.
To find first two maximum numbers in an array,using single loop without sorting array.  Java program to find first two maximum numbers in an array,using single loop without sorting array
Listing all even numbers between two numbers
Listing all even numbers between two numbers  Hi, How to write code to list all the even numbers between two given numbers? Thanks   Hi... the numbers. Check the tutorial Write a program to list all even numbers between two
); add(text2); label3 = new Label("Sum of Two Numbers...Applet for add two numbers  what is the java applet code for add two numbers?   import java.awt.Graphics; import javax.swing.*; public
Swapping of two numbers without using third variable
Swapping of two numbers without using third variable In this tutorial we will learn about swapping of two number in java without using third variable. This is simple program to swap two value using arithmetic operation Swapping of two
Java Bitwise Operator
; In computer, the bitwise operators as their name suggests actually work... (or characters). However in Java, the bitwise and bit shift operators are used.... These operators perform bitwise and bit shift operations on integral type
Swap two numbers without using third variable
Swap two numbers without using third variable In this section we are going to swap two variables without using the third variable. For this, we have used... from the command prompt. Instead of using temporary variable, we have done some
Add two big numbers       In this section, you will learn how to add two big numbers. For adding two numbers implement two big decimal numbers then apply the Sum() method
Add two big numbers - Java Beginners
Add two big numbers - Java Beginners  Hi, I am beginner in Java and leaned basic concepts of Java. Now I am trying to find example code for adding big numbers in Java. I need basic Java Beginners example. It should easy
Swapping of two numbers in java
Swapping of two numbers in java In this example we are going to describe swapping of two numbers in java  without using the third number in java. We... values from the command prompt. The swapping of two numbers is based on simple
To find first two maximum numbers in an array
To find first two maximum numbers in an array  Java program to find first two maximum numbers in an array,using single loop without sorting array
mysql difference between two numbers
mysql difference between two numbers  How to get total bate difference between two dates for example 1/01/2012 and 1/02/2012 in MYSQL?   ... between two date. The syntax of DATEDIFF is .. SELECT DATEDIFF('2012-01-31 23:59
Operators
to a specified type  Bitwise and Bit Shift Operators ~   Unary bitwise... Operators       Operators are symbols that performs some operations on one or more then one operands
Operators
to a specified type  Bitwise and Bit Shift Operators ~   Unary bitwise... Operators       Operators are symbols that performs some operations on one or more then one operands
Operators and Expressions
to perform certain tasks like assigning a value, adding the numbers etc. Java operators... Bitwise and Bit Shift Operators... Operators and Expressions
Comparing Two Numbers
Comparing Two Numbers      ... of comparing two numbers and finding out the greater one. First of all, name a class "Comparing" and take two numbers in this class. Here we have taken a=24
Swapping of two numbers
Swapping of two numbers      ... program to calculate swap of two numbers. Swapping is used where  you want... ability. In this program we will see how we can swap two numbers. We can do
Operators
to a specified type  Bitwise and Bit Shift Operators ~   Unary bitwise... Operators       Operators are symbols that performs some operations on one or more than one operands
Operators
to a specified type  Bitwise and Bit Shift Operators ~   Unary bitwise... Operators       Operators are symbols that performs some operations on one or more then one operands
Tutorials