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