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