Skip to content

Commit ecf8bd7

Browse files
sumitsg34maibin
authored andcommitted
BAEL-3027 binary number arithmetic operations (eugenp#7437)
* 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
1 parent db90a53 commit ecf8bd7

2 files changed

Lines changed: 221 additions & 0 deletions

File tree

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.baeldung.binarynumbers;
2+
3+
public class BinaryNumbers {
4+
5+
/**
6+
* This method takes a decimal number and convert it into a binary number.
7+
* example:- input:10, output:1010
8+
*
9+
* @param decimalNumber
10+
* @return binary number
11+
*/
12+
public Integer convertDecimalToBinary(Integer decimalNumber) {
13+
14+
if (decimalNumber == 0) {
15+
return decimalNumber;
16+
}
17+
18+
StringBuilder binaryNumber = new StringBuilder();
19+
20+
while (decimalNumber > 0) {
21+
22+
int remainder = decimalNumber % 2;
23+
int result = decimalNumber / 2;
24+
25+
binaryNumber.append(remainder);
26+
decimalNumber = result;
27+
}
28+
29+
binaryNumber = binaryNumber.reverse();
30+
31+
return Integer.valueOf(binaryNumber.toString());
32+
}
33+
34+
/**
35+
* This method takes a binary number and convert it into a decimal number.
36+
* example:- input:101, output:5
37+
*
38+
* @param binary number
39+
* @return decimal Number
40+
*/
41+
public Integer convertBinaryToDecimal(Integer binaryNumber) {
42+
43+
Integer result = 0;
44+
Integer base = 1;
45+
46+
while (binaryNumber > 0) {
47+
48+
int lastDigit = binaryNumber % 10;
49+
binaryNumber = binaryNumber / 10;
50+
51+
result += lastDigit * base;
52+
53+
base = base * 2;
54+
}
55+
return result;
56+
}
57+
58+
/**
59+
* This method accepts two binary numbers and returns sum of input numbers.
60+
* Example:- firstNum: 101, secondNum: 100, output: 1001
61+
*
62+
* @param firstNum
63+
* @param secondNum
64+
* @return addition of input numbers
65+
*/
66+
public Integer addBinaryNumber(Integer firstNum, Integer secondNum) {
67+
68+
StringBuilder output = new StringBuilder();
69+
70+
int carry = 0;
71+
int temp;
72+
73+
while (firstNum != 0 || secondNum != 0) {
74+
75+
temp = (firstNum % 10 + secondNum % 10 + carry) % 2;
76+
output.append(temp);
77+
78+
carry = (firstNum % 10 + secondNum % 10 + carry) / 2;
79+
80+
firstNum = firstNum / 10;
81+
secondNum = secondNum / 10;
82+
}
83+
84+
if (carry != 0) {
85+
output.append(carry);
86+
}
87+
88+
return Integer.valueOf(output.reverse()
89+
.toString());
90+
}
91+
92+
/**
93+
* This method takes two binary number as input and subtract second number from the first number.
94+
* example:- firstNum: 1000, secondNum: 11, output: 101
95+
* @param firstNum
96+
* @param secondNum
97+
* @return Result of subtraction of secondNum from first
98+
*/
99+
public Integer substractBinaryNumber(Integer firstNum, Integer secondNum) {
100+
101+
int onesComplement = Integer.valueOf(getOnesComplement(secondNum));
102+
103+
StringBuilder output = new StringBuilder();
104+
105+
int carry = 0;
106+
int temp;
107+
108+
while (firstNum != 0 || onesComplement != 0) {
109+
110+
temp = (firstNum % 10 + onesComplement % 10 + carry) % 2;
111+
output.append(temp);
112+
113+
carry = (firstNum % 10 + onesComplement % 10 + carry) / 2;
114+
115+
firstNum = firstNum / 10;
116+
onesComplement = onesComplement / 10;
117+
}
118+
119+
String additionOfFirstNumAndOnesComplement = output.reverse()
120+
.toString();
121+
122+
if (carry == 1) {
123+
return addBinaryNumber(Integer.valueOf(additionOfFirstNumAndOnesComplement), carry);
124+
} else {
125+
return getOnesComplement(Integer.valueOf(additionOfFirstNumAndOnesComplement));
126+
}
127+
128+
}
129+
130+
public Integer getOnesComplement(Integer num) {
131+
132+
StringBuilder onesComplement = new StringBuilder();
133+
134+
while (num > 0) {
135+
int lastDigit = num % 10;
136+
if (lastDigit == 0) {
137+
onesComplement.append(1);
138+
} else {
139+
onesComplement.append(0);
140+
}
141+
num = num / 10;
142+
}
143+
144+
return Integer.valueOf(onesComplement.reverse()
145+
.toString());
146+
}
147+
148+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.baeldung.binarynumbers;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Test;
6+
7+
public class BinaryNumbersUnitTest {
8+
9+
private BinaryNumbers binaryNumbers = new BinaryNumbers();
10+
11+
@Test
12+
public void given_decimalNumber_then_returnBinaryNumber() {
13+
assertEquals(Integer.valueOf(1000), binaryNumbers.convertDecimalToBinary(8));
14+
assertEquals(Integer.valueOf(10100), binaryNumbers.convertDecimalToBinary(20));
15+
}
16+
17+
@Test
18+
public void given_decimalNumber_then_convertToBinaryNumber() {
19+
assertEquals("1000", Integer.toBinaryString(8));
20+
assertEquals("10100", Integer.toBinaryString(20));
21+
}
22+
23+
@Test
24+
public void given_binaryNumber_then_ConvertToDecimalNumber() {
25+
assertEquals(8, Integer.parseInt("1000", 2));
26+
assertEquals(20, Integer.parseInt("10100", 2));
27+
}
28+
29+
@Test
30+
public void given_binaryNumber_then_returnDecimalNumber() {
31+
assertEquals(Integer.valueOf(8), binaryNumbers.convertBinaryToDecimal(1000));
32+
assertEquals(Integer.valueOf(20), binaryNumbers.convertBinaryToDecimal(10100));
33+
}
34+
35+
@Test
36+
public void given_twoBinaryNumber_then_returnAddition() {
37+
// adding 4 and 10
38+
assertEquals(Integer.valueOf(1110), binaryNumbers.addBinaryNumber(100, 1010));
39+
40+
// adding 26 and 14
41+
assertEquals(Integer.valueOf(101000), binaryNumbers.addBinaryNumber(11010, 1110));
42+
}
43+
44+
@Test
45+
public void given_twoBinaryNumber_then_returnSubtraction() {
46+
// subtracting 16 from 25
47+
assertEquals(Integer.valueOf(1001), binaryNumbers.substractBinaryNumber(11001, 10000));
48+
49+
// subtracting 29 from 16, the output here is negative
50+
assertEquals(Integer.valueOf(1101), binaryNumbers.substractBinaryNumber(10000, 11101));
51+
}
52+
53+
@Test
54+
public void given_binaryLiteral_thenReturnDecimalValue() {
55+
56+
byte five = 0b101;
57+
assertEquals((byte) 5, five);
58+
59+
short three = 0b11;
60+
assertEquals((short) 3, three);
61+
62+
int nine = 0B1001;
63+
assertEquals(9, nine);
64+
65+
long twentyNine = 0B11101;
66+
assertEquals(29, twentyNine);
67+
68+
int minusThirtySeven = -0B100101;
69+
assertEquals(-37, minusThirtySeven);
70+
71+
}
72+
73+
}

0 commit comments

Comments
 (0)