Wednesday 10 September 2014

Java



Write a program to addd two integer without using + or any arithmetic operator


public static int get_sum(int a, int b){

 if(b==0) return a;

 int sum = a^b; //add without carry

 it carry = (a&b)<<1; //carry,but don't add


 return get_sum(sum,carry);


}




Write a program to swap two integer without using third variable or any arithmetic operator



public static void main(String[] args){

//Supose there are two int a=5 b=4

int a = 5; /*a=100*/
int b = 4; /*b=101*/
a=a^b; /* 100^101 = 001*/
b=a^b; /* 001^101 = 100*/
a=a^b; /*001^100 = 101*/

System.out.println("Value of a="+a);
System.out.println("Value of a="+a);

}


1 comment:

  1. hmmm 3rd method to swap two numbers.. gr8 :)

    ReplyDelete

Enable 'HTML Manager'- Apache Tomcat

 Enable 'HTML Manager'- Apache Tomcat Go to Apache-tomcat's home directory and edit the tomcat-users.xml ${apache_home}/conf/tom...