From ab0477a4b29115a18229c33b5532edb99b7abf5c Mon Sep 17 00:00:00 2001 From: Sumeet Gajbhar Date: Tue, 6 Aug 2019 01:55:25 +0530 Subject: [PATCH] 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 --- .../baeldung/binarynumbers/BinaryNumbers.java | 288 +++++++++--------- 1 file changed, 140 insertions(+), 148 deletions(-) diff --git a/java-numbers-2/src/main/java/com/baeldung/binarynumbers/BinaryNumbers.java b/java-numbers-2/src/main/java/com/baeldung/binarynumbers/BinaryNumbers.java index effdee07ad..62cead726f 100644 --- a/java-numbers-2/src/main/java/com/baeldung/binarynumbers/BinaryNumbers.java +++ b/java-numbers-2/src/main/java/com/baeldung/binarynumbers/BinaryNumbers.java @@ -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()); + } + +} \ No newline at end of file