Last active
June 22, 2017 16:24
-
-
Save anil477/10eefc4a6c686009576e042c0538c51e to your computer and use it in GitHub Desktop.
Bitwise Addition, Decimal to Binary,Binary to Decimal, Add without using arithmetic operator, Add without Arithmetic
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/ | |
// http://www.geeksforgeeks.org/add-two-bit-strings/ | |
// Decimal to Binary | |
// http://www.geeksforgeeks.org/add-two-numbers-without-using-arithmetic-operators/ | |
import java.lang.*; | |
class BitwiseAddition { | |
public void add(String s1, String s2) { | |
int l = s1.length(); | |
int sum; | |
int c = 0; | |
int c1, c2; | |
StringBuilder result = new StringBuilder(""); | |
for(int i=l-1; i >=0; i--){ | |
c1 = s1.charAt(i) - '0'; // to convert char to int - 0 | |
c2 = s2.charAt(i) - '0'; | |
sum = c1 ^ c2 ^ c; | |
c = (c1 & c2) | ( c2 & c ) | ( c1 & c ); | |
result.append(sum); | |
} | |
if(c > 0) | |
result.append(1); | |
System.out.println(result.reverse()); | |
} | |
public void DecimalToBinary(int n) | |
{ | |
int r; | |
StringBuilder result = new StringBuilder(""); | |
while(n>0) | |
{ | |
r = n%2; | |
result.insert(0, r); | |
n = n/2; | |
} | |
System.out.println(result); | |
} | |
// add without using arithmetic operator | |
public void add(int x, int y) | |
{ | |
// Iterate till there is no carry | |
while (y != 0) | |
{ | |
// carry now contains common set bits of x and y | |
int carry = x & y; | |
// System.out.println(" Carry " + carry); | |
// 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 << 1; | |
} | |
System.out.println(x); | |
} | |
public void addWithoutArithmetic(int x, int y) | |
{ | |
int carry; | |
// Iterate till there is no carry | |
while (y != 0) | |
{ | |
// carry now contains common set bits of x and y | |
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 << 1; | |
} | |
System.out.println(x); | |
} | |
public void decimalToBinary(int n) | |
{ | |
int result = 0; | |
int j = 0; | |
int temp = 0; | |
while(n>0) | |
{ | |
temp = n % 10; | |
result = result + temp * (int)(Math.pow(2, j)); | |
j++; | |
n = n/10; | |
} | |
System.out.println(" Number: " + result); | |
} | |
public static void main(String args[]) { | |
BitwiseAddition obj = new BitwiseAddition(); | |
// when length is not same do 0 padding to make length the same | |
String s1 = "1100011"; | |
String s2 = "0000010"; | |
// obj.add(s1, s2); | |
//obj.DecimalToBinary(10); | |
//obj.add(10,40); | |
//obj.addWithoutArithmetic(12,10); | |
obj.decimalToBinary(1100); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment