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
|
@ -16,18 +16,16 @@ public class BinaryNumbers {
|
|||
}
|
||||
|
||||
StringBuilder binaryNumber = new StringBuilder();
|
||||
Integer quotient = decimalNumber;
|
||||
|
||||
while (decimalNumber > 0) {
|
||||
|
||||
int remainder = decimalNumber % 2;
|
||||
int result = decimalNumber / 2;
|
||||
while (quotient > 0) {
|
||||
|
||||
int remainder = quotient % 2;
|
||||
binaryNumber.append(remainder);
|
||||
decimalNumber = result;
|
||||
quotient /= 2;
|
||||
}
|
||||
|
||||
binaryNumber = binaryNumber.reverse();
|
||||
|
||||
return Integer.valueOf(binaryNumber.toString());
|
||||
}
|
||||
|
||||
|
@ -40,7 +38,7 @@ public class BinaryNumbers {
|
|||
*/
|
||||
public Integer convertBinaryToDecimal(Integer binaryNumber) {
|
||||
|
||||
Integer result = 0;
|
||||
Integer decimalNumber = 0;
|
||||
Integer base = 1;
|
||||
|
||||
while (binaryNumber > 0) {
|
||||
|
@ -48,11 +46,10 @@ public class BinaryNumbers {
|
|||
int lastDigit = binaryNumber % 10;
|
||||
binaryNumber = binaryNumber / 10;
|
||||
|
||||
result += lastDigit * base;
|
||||
|
||||
decimalNumber += lastDigit * base;
|
||||
base = base * 2;
|
||||
}
|
||||
return result;
|
||||
return decimalNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -99,9 +96,7 @@ public class BinaryNumbers {
|
|||
public Integer substractBinaryNumber(Integer firstNum, Integer secondNum) {
|
||||
|
||||
int onesComplement = Integer.valueOf(getOnesComplement(secondNum));
|
||||
|
||||
StringBuilder output = new StringBuilder();
|
||||
|
||||
int carry = 0;
|
||||
int temp;
|
||||
|
||||
|
@ -124,13 +119,11 @@ public class BinaryNumbers {
|
|||
} 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) {
|
||||
|
@ -140,7 +133,6 @@ public class BinaryNumbers {
|
|||
}
|
||||
num = num / 10;
|
||||
}
|
||||
|
||||
return Integer.valueOf(onesComplement.reverse()
|
||||
.toString());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue