BAEL-3127 Binary Numbers in Java (#7469)

* BAEL-3027 binary number arithmetic operations

* BAEL-3127 updated the unit test case names

* added unit test cases for built-in java functions

* updated function name

* BAEL-3127 updated unit test case names

* BAEL-3127 removed comments from code

* BAEL-3127 updated variable names

* BAEL-3027 binary number arithmetic operations

* merging upsteam

* fixing build issue

* updated formatting

* update variable names
This commit is contained in:
Sumeet Gajbhar 2019-08-06 01:55:25 +05:30 committed by maibin
parent 780d6c765c
commit ab0477a4b2
1 changed files with 140 additions and 148 deletions

View File

@ -1,148 +1,140 @@
package com.baeldung.binarynumbers;
public class BinaryNumbers {
/**
* This method takes a decimal number and convert it into a binary number.
* example:- input:10, output:1010
*
* @param decimalNumber
* @return binary number
*/
public Integer convertDecimalToBinary(Integer decimalNumber) {
if (decimalNumber == 0) {
return decimalNumber;
}
StringBuilder binaryNumber = new StringBuilder();
while (decimalNumber > 0) {
int remainder = decimalNumber % 2;
int result = decimalNumber / 2;
binaryNumber.append(remainder);
decimalNumber = result;
}
binaryNumber = binaryNumber.reverse();
return Integer.valueOf(binaryNumber.toString());
}
/**
* This method takes a binary number and convert it into a decimal number.
* example:- input:101, output:5
*
* @param binary number
* @return decimal Number
*/
public Integer convertBinaryToDecimal(Integer binaryNumber) {
Integer result = 0;
Integer base = 1;
while (binaryNumber > 0) {
int lastDigit = binaryNumber % 10;
binaryNumber = binaryNumber / 10;
result += lastDigit * base;
base = base * 2;
}
return result;
}
/**
* This method accepts two binary numbers and returns sum of input numbers.
* Example:- firstNum: 101, secondNum: 100, output: 1001
*
* @param firstNum
* @param secondNum
* @return addition of input numbers
*/
public Integer addBinaryNumber(Integer firstNum, Integer secondNum) {
StringBuilder output = new StringBuilder();
int carry = 0;
int temp;
while (firstNum != 0 || secondNum != 0) {
temp = (firstNum % 10 + secondNum % 10 + carry) % 2;
output.append(temp);
carry = (firstNum % 10 + secondNum % 10 + carry) / 2;
firstNum = firstNum / 10;
secondNum = secondNum / 10;
}
if (carry != 0) {
output.append(carry);
}
return Integer.valueOf(output.reverse()
.toString());
}
/**
* This method takes two binary number as input and subtract second number from the first number.
* example:- firstNum: 1000, secondNum: 11, output: 101
* @param firstNum
* @param secondNum
* @return Result of subtraction of secondNum from first
*/
public Integer substractBinaryNumber(Integer firstNum, Integer secondNum) {
int onesComplement = Integer.valueOf(getOnesComplement(secondNum));
StringBuilder output = new StringBuilder();
int carry = 0;
int temp;
while (firstNum != 0 || onesComplement != 0) {
temp = (firstNum % 10 + onesComplement % 10 + carry) % 2;
output.append(temp);
carry = (firstNum % 10 + onesComplement % 10 + carry) / 2;
firstNum = firstNum / 10;
onesComplement = onesComplement / 10;
}
String additionOfFirstNumAndOnesComplement = output.reverse()
.toString();
if (carry == 1) {
return addBinaryNumber(Integer.valueOf(additionOfFirstNumAndOnesComplement), carry);
} else {
return getOnesComplement(Integer.valueOf(additionOfFirstNumAndOnesComplement));
}
}
public Integer getOnesComplement(Integer num) {
StringBuilder onesComplement = new StringBuilder();
while (num > 0) {
int lastDigit = num % 10;
if (lastDigit == 0) {
onesComplement.append(1);
} else {
onesComplement.append(0);
}
num = num / 10;
}
return Integer.valueOf(onesComplement.reverse()
.toString());
}
}
package com.baeldung.binarynumbers;
public class BinaryNumbers {
/**
* This method takes a decimal number and convert it into a binary number.
* example:- input:10, output:1010
*
* @param decimalNumber
* @return binary number
*/
public Integer convertDecimalToBinary(Integer decimalNumber) {
if (decimalNumber == 0) {
return decimalNumber;
}
StringBuilder binaryNumber = new StringBuilder();
Integer quotient = decimalNumber;
while (quotient > 0) {
int remainder = quotient % 2;
binaryNumber.append(remainder);
quotient /= 2;
}
binaryNumber = binaryNumber.reverse();
return Integer.valueOf(binaryNumber.toString());
}
/**
* This method takes a binary number and convert it into a decimal number.
* example:- input:101, output:5
*
* @param binary number
* @return decimal Number
*/
public Integer convertBinaryToDecimal(Integer binaryNumber) {
Integer decimalNumber = 0;
Integer base = 1;
while (binaryNumber > 0) {
int lastDigit = binaryNumber % 10;
binaryNumber = binaryNumber / 10;
decimalNumber += lastDigit * base;
base = base * 2;
}
return decimalNumber;
}
/**
* This method accepts two binary numbers and returns sum of input numbers.
* Example:- firstNum: 101, secondNum: 100, output: 1001
*
* @param firstNum
* @param secondNum
* @return addition of input numbers
*/
public Integer addBinaryNumber(Integer firstNum, Integer secondNum) {
StringBuilder output = new StringBuilder();
int carry = 0;
int temp;
while (firstNum != 0 || secondNum != 0) {
temp = (firstNum % 10 + secondNum % 10 + carry) % 2;
output.append(temp);
carry = (firstNum % 10 + secondNum % 10 + carry) / 2;
firstNum = firstNum / 10;
secondNum = secondNum / 10;
}
if (carry != 0) {
output.append(carry);
}
return Integer.valueOf(output.reverse()
.toString());
}
/**
* This method takes two binary number as input and subtract second number from the first number.
* example:- firstNum: 1000, secondNum: 11, output: 101
* @param firstNum
* @param secondNum
* @return Result of subtraction of secondNum from first
*/
public Integer substractBinaryNumber(Integer firstNum, Integer secondNum) {
int onesComplement = Integer.valueOf(getOnesComplement(secondNum));
StringBuilder output = new StringBuilder();
int carry = 0;
int temp;
while (firstNum != 0 || onesComplement != 0) {
temp = (firstNum % 10 + onesComplement % 10 + carry) % 2;
output.append(temp);
carry = (firstNum % 10 + onesComplement % 10 + carry) / 2;
firstNum = firstNum / 10;
onesComplement = onesComplement / 10;
}
String additionOfFirstNumAndOnesComplement = output.reverse()
.toString();
if (carry == 1) {
return addBinaryNumber(Integer.valueOf(additionOfFirstNumAndOnesComplement), carry);
} else {
return getOnesComplement(Integer.valueOf(additionOfFirstNumAndOnesComplement));
}
}
public Integer getOnesComplement(Integer num) {
StringBuilder onesComplement = new StringBuilder();
while (num > 0) {
int lastDigit = num % 10;
if (lastDigit == 0) {
onesComplement.append(1);
} else {
onesComplement.append(0);
}
num = num / 10;
}
return Integer.valueOf(onesComplement.reverse()
.toString());
}
}