Expedia Interview Question
Software Engineer in TestsCountry: United States
Here is the code to mulitply two numbers using the bitwise opertators
vid mulitply_two_numbers(int n,int m){
int i=0;
int res = 0;
for(i=0;i< 32; i++){
if(n & 1 << i)
res= res + (m<<i);
}
printf(" %d*%d = %d", n,m, res);
}
package test;
import java.util.Stack;
public class BitWiseMultiplication {
static int bit(int num) {
StringBuffer sb = new StringBuffer();
Stack<Integer> stack = new Stack<Integer>();
while(num>=1) {
stack.push(num%2);
num = num/2;
}
while(!stack.isEmpty()) {
sb.append(stack.pop());
}
return Integer.valueOf(sb.toString());
}
static int bitAdd(int a, int b) {
Stack<Integer> stack = new Stack<Integer>();
int remainder = 0;
while (a>=1 || b>=1) {
int temp = (a%10)+(b%10)+remainder;
if (temp <= 1) {
stack.push(temp);
} else {
stack.push(0);
remainder=1;
}
a=a/10;
b=b/10;
}
StringBuffer sb = new StringBuffer();
while(!stack.isEmpty()) {
sb.append(stack.pop());
}
return Integer.valueOf(sb.toString());
}
static int convertBitToInt(int bitNum) {
int i=0;
int result = 0;
while(bitNum>=1) {
result += Math.pow(2, i)*(bitNum%10);
bitNum = bitNum/10;
i++;
}
return result;
}
public static void main(String[] args) {
int a = 9;
int b = 2;
int bitA = bit(a);
int bitB = bit(b);
System.out.println("Num A:: "+a);
System.out.println("Num B:: "+b);
System.out.println("Bit A:: "+bitA);
System.out.println("Bit B:: "+bitB);
int bitMulResult = bitA*bitB;
System.out.println("Bit Mul Result:: "+bitMulResult);
int bitAddResult = bitAdd(bitA, bitB);
System.out.println("Bit Add Result:: "+bitAddResult);
int mulResult = convertBitToInt(bitMulResult);
System.out.println("Int Mul Result:: "+mulResult);
int addResult = convertBitToInt(bitAddResult);
System.out.println("Int Result:: "+addResult);
}
}
package test;
import java.util.Stack;
public class BitWiseOperations {
static int bit(int num) {
StringBuffer sb = new StringBuffer();
Stack<Integer> stack = new Stack<Integer>();
while(num>=1) {
stack.push(num%2);
num = num/2;
}
while(!stack.isEmpty()) {
sb.append(stack.pop());
}
return Integer.valueOf(sb.toString());
}
static int bitAdd(int a, int b) {
Stack<Integer> stack = new Stack<Integer>();
int remainder = 0;
while (a>=1 || b>=1) {
int temp = (a%10)+(b%10)+remainder;
remainder=0;
if (temp <= 1) {
stack.push(temp);
} else {
stack.push(0);
remainder=1;
}
a=a/10;
b=b/10;
}
if (remainder == 1) {
stack.push(1);
}
StringBuffer sb = new StringBuffer();
while(!stack.isEmpty()) {
sb.append(stack.pop());
}
return Integer.valueOf(sb.toString());
}
static int convertBitToInt(int bitNum) {
int i=0;
int result = 0;
while(bitNum>=1) {
result += Math.pow(2, i)*(bitNum%10);
bitNum = bitNum/10;
i++;
}
return result;
}
public static void main(String[] args) {
int a = 4;
int b = 4;
int bitA = bit(a);
int bitB = bit(b);
System.out.println("Num A:: "+a);
System.out.println("Num B:: "+b);
System.out.println("Bit A:: "+bitA);
System.out.println("Bit B:: "+bitB);
int bitMulResult = bitA*bitB;
System.out.println("Bit Mul Result:: "+bitMulResult);
int bitAddResult = bitAdd(bitA, bitB);
System.out.println("Bit Add Result:: "+bitAddResult);
int mulResult = convertBitToInt(bitMulResult);
System.out.println("Int Mul Result:: "+mulResult);
int addResult = convertBitToInt(bitAddResult);
System.out.println("Int Result:: "+addResult);
}
}
Bitwise Multiplication
public int Multiplication(int i, int j)
{
// take small number as 2nd number to make it faster
if (i < j)
{
int temp = i;
i = j;
j = temp;
}
int totalBits = 0;
int m = 0;
// calculate bits in 2nd number
while (Math.Pow(2, m) < j)
{
totalBits++;
m++;
}
int sum = 0;
int carry = 0;
for (int n = 0; n < totalBits; n++)
{
// Making AND between 2nd number and numbers 1, 2, 4, 8 .. to detect whether bit in a specific location in 2nd number is '0' or '1'
int digit = j & (int)Math.Pow(2, n);
//If bit in 2nd number is '1' then only do below operation
if(digit > 0)
{
// this is considering both numbers are 8 bits
digit = 255;
// Below operation is same when we do a decimal multiplication
carry = ((digit & i) << n) & sum;
sum = ((digit & i) << n) ^ sum;
while (carry != 0)
{
int newCarry = sum & (carry << 1);
sum = sum ^ (carry << 1);
carry = newCarry;
}
}
}
return sum;
}
- Chandan Singh September 28, 2012