Google
Company
General Ability
General Knowledge
implement adding twounsighned numbers without using + or ++
Read Solution (Total 10)
-
- suppose two numbers a and b then a-(-b)=sum of a and b;
- 12 years agoHelpfull: Yes(10) No(4)
- As we know a^2-b^2= (a+b)(a-b)
so a+b= (a^2-b^2) / (a-b)
and 2nd one is
int add (int a, int b)
{
int c, d;
if (b == 0)
{
return a;
}
c = a ^ b; // xor takes sum
d = a & b; // collect carry;
d = d - 12 years agoHelpfull: Yes(8) No(2)
- if x and y are the two unsigned numbers, then
x+y = (x*x-y*y)/(x-y)
eg: x=5 and y=2
5+2=7
(x*x-y*y)/(x-y)=(5*5-2*2)/(5-2)
=(25-4)/3
=21/3
=7
- 12 years agoHelpfull: Yes(4) No(0)
- use or between 2 unsigned numbers
- 10 years agoHelpfull: Yes(1) No(0)
- 1. Take first number, multiply it with -1.
2. Subtract second number from step 1.
3. Multiply the result with -1 again.
2 numbers are added without using + or ++. - 7 years agoHelpfull: Yes(1) No(0)
- Sum of two bits can be obtained by performing XOR (^) of the two bits. Carry bit can be obtained by performing AND (&) of two bits
int Add(int x, int y)
{
if (y == 0)
return x;
else
return Add( x ^ y, (x & y) - 11 years agoHelpfull: Yes(0) No(0)
- Perform logical OR operation.
- 10 years agoHelpfull: Yes(0) No(0)
- #include
int Add(int x, int y)
{
// Iterate till there is no carry
while (y != 0)
{
int carry = x & y;
// Sum of bits of x and y where at least one of the bits is not set
x = x ^ y;
// Carry is shifted by one so that adding it to x gives the required sum
y = carry - 8 years agoHelpfull: Yes(0) No(0)
- use or between 2 unsigned numbers
- 7 years agoHelpfull: Yes(0) No(0)
- we can add two unsigned numbers without using +or ++
for ex :
import java.util.scanner
class Add
{
public static void main(String[] arg)
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter first number");
a=sc.nextInt();
System.out.println("Enter second number");
b=sc.nextInt();
c=addition(a,b);
System.out.println(" Addition of two numbers is : "+c);
}
static int addition(int x,int y)
{
return x+y;
}
} - 5 years agoHelpfull: Yes(0) No(0)
Google Other Question