Performing mathematical operation's without using bitwise operator in java.

__Program:-__
public class WithoutOperator {

public static void main(String[] args) {

System.out.println(add(3,10));

System.out.println(subtract(13,10));

System.out.println(multiple(190,11));

System.out.println(divide(124,11));

swap(12,89);

System.out.println(abs(-1109));

System.out.println(abs(1109));

System.out.println(small(10,90));

System.out.println(small(10,9));

System.out.println(small(1,90));

System.out.println(small(10,10));

System.out.println(isEven(111));

}

public static int add(int first,int secon){

while (secon != 0)

{

int carry =first & secon;

first = first ^ secon;

secon = carry << 1;

}

return first;

}

public static int subtract(int first,int secon){

return (add(first, add(~secon, 1)));

}

public static int multiple(int first,int secon){

int i=0,m=0,k;

while(secon>0)

{

k=secon%2;

if(k==1)

m+=first<<i;

i++;

secon/=2;

}

return m;

}

public static int divide(int numerator, int denominator)

{

int mask = 0x1;

int quotient = 0;

while (denominator <= numerator) {

denominator <<= 1;

mask <<= 1;

}

while (mask > 1) {

denominator >>= 1;

mask >>= 1;

if (numerator >= denominator) {

numerator -= denominator;

quotient |= mask;

}

}

return quotient;

}

public static void swap(int first,int secon){

first=first^secon;

secon=first^secon;

first=first^secon;

System.out.println("First: " + first + " Second : " + secon);

}

public static int abs(int first){

return (first^((~((first>>31)&1))+1)) + ((first>>31)&1);

}

public static int small(int first,int secon){

int r= first ^ ((first ^ secon) & -(first << secon));

return r;

}

public static boolean isEven(int no){

return ((no&1)==0); //Anding the right most bit in the given number with 1

}

}

**Addition:-**
1) consider two number first=10 and secon=3.

2) Binary representation of the number :

first : 01010

secon: 00011

a)Check the secon is 0 or not.If it's 0 then return first.else perform addition in fallowing ways.

b)Get the carry bit by andding the first and second number .

first : 01010

secon: 00011 &

----------------

carry : 00010 -2

----------------

c) XOR first and second number.

first : 01010

secon: 00011 XOR

--------------------

first : 01001 - 9

--------------------

d) Shift 1 bit left to make add carry to next time.

carry: 00010 <<1

------------------

secon: 00100

-----------------

e) Continue the process until reminder get's 0.

In steps:

carry : 00000 (0)

first : 01101 (13)

secon: 00000 (0)

*add method : *
public static int add(int first,int secon){

while (secon != 0)

{

// carry now contains common set bits of first and secon

int carry =first & secon;

// Sum of bits of first and secon where at least one of the bits is not set

first = first ^ secon;

// Carry is shifted by one so that adding it to first gives the required sum

secon = carry << 1;

}

return first;

}

**Subtraction :**
1) Add - sign to second number using not operator.when we use not sign then It will stores as 1's compliment + 1. so we have add 1 to the negative number to get proper negative number.

so that we use add(-secon,1) and add the return negative value of the number to first to get subtract.

*subtract method:*
public static int subtract(int first,int secon){

return (add(first, add(~secon, 1)));

}

**Multiplication :**
1) If second number greater then 0 then perform this operation.

2) Check second number is even or not.If that is odd then shift the element left by i times,else do nothing.

3) Increment i and store second number by 2 of it's.

*Multiplication method :*
public static int multiple(int first,int secon){

int i=0,m=0,k;

while(secon>0)

{

k=secon%2;

if(k==1)

m+=first<<i;

i++;

secon/=2;

}

return m;

}

**Division:**
Click here to refer division.

*Divide method:*
Public static int divide(int numerator, int denominator)

{

int mask = 0x1;

int quotient = 0;

while (denominator <= numerator) {

denominator <<= 1;

mask <<= 1;

}

while (mask > 1) {

denominator >>= 1;

mask >>= 1;

if (numerator >= denominator) {

numerator -= denominator;

quotient |= mask;

}

}

return quotient;

}

**Swap two number :**

Already we discuss this topic in this blog. Click here to go to the page.

*Swap method:*

public static void swap(int first,int secon){

first=first^secon;

secon=first^secon;

first=first^secon;

System.out.println("First: " + first + " Second : " + secon);

}

**Absolute :**

It is for 32 bit integer operation.If the given number is negative then first bit of the number is 1.if that number is positive first bit is 0.

Right shift the given number to 32 times and then and-ding with 1 we get 1 if negative else 0 for the positive.and add 1 to the result based on the previous result.and not the result get's negative(computer stores it in 2's complement) and -1 for negative number and 0 for positive number.

when XOR the number with the -1(1111111....0) we get signed bit 0. and we get positive number with on reduced and finally add the number with reduced amount by previous operation we discussed.

*Absolute method :*
public static int abs(int first){

return (first^((~((first>>31)&1))+1)) + ((first>>31)&1);

}

**Minimum number:**
XOR first and second number and with - of the left shifted of first with second times and XOR with the first number to get minimum number.

*Minimum function ::*
public static int small(int first,int secon){

int r= first ^ ((first ^ secon) & -(first << secon));

return r;

}

**Iseven number :**
Negative number stores in 2's complement in memory and 'AND' with 1 get compared with right most bit and return the result 1 for odd and 0 for even.

*Note:-* Always odd number has 1 in right most bit.

*Iseven method:*
public static boolean isEven(int no){

return ((no&1)==0); //Anding the right most bit in the given number with 1

}