Write a Java/C++ program to perform binary addition and subtraction with 8-bit s
ID: 3666798 • Letter: W
Question
Write a Java/C++ program to perform binary addition and subtraction with 8-bit signed binary numbers. You are to write a menu driven program that gives the user three choices, namely, (1) Add, (2) Subtract, or (3) Exit. The input to your program will be two signed binary numbers. The program should display the input/output numbers in binary. Your program should be well documented and in particular, begin with a series of comments specifying: your name, course #, semester and other relevant information a brief description of what the program does. Print out a copy of the program along with the output. Submit this printout for grading.Explanation / Answer
//header files
import java.util.Scanner;
// create class binary
public class binary
{
private static final String MENU = "****Menu**** " +
"1 -> Add " +
"2 -> Subtract " +
"0 -> quit " +
">";
public static void main(String... args)
{
//getting input from user
Scanner keyboard = new Scanner(System.in);
while (true)
{
String byteString;
String byteString2;
System.out.print(MENU);
switch (keyboard.nextInt())
{
case 0:
System.out.println("Goodbye!");
System.exit(0);
break;
//choice =2
//adding two binary number
case 1:
System.out.print("Enter 8 bit signed binary number: ");
byteString = cleanInput(keyboard.next());
System.out.print("Enter another binary Number> ");
byteString2 = cleanInput(keyboard.next());
if(byteString.contains(" ") || byteString.length() > 8)
{
System.out.println(byteString + " is not a valid 8 bit binary number, try again.");
return;
}
String sum = add(byteString, byteString2);
System.out.print(sum + "(");
byteToDec(sum, 0);
System.out.println(")");
break;
//subtract to binary number
case 2:
System.out.print("Enter 8 bit signed binary number: ");
byteString = cleanInput(keyboard.next());
System.out.print("Enter another binary Number> ");
byteString2 = cleanInput(keyboard.next());
if(byteString.contains(" ") || byteString.length() > 8)
{
System.out.println(byteString + " is not a valid 8 bit binary number, try again.");
return;
}
String difference = subtract(byteString, byteString2);
System.out.print(difference + "(");
byteToDec(difference, 0);
System.out.println(")");
break;
default:
System.out.println("Invalid command, try again.");
break;
}
System.out.println();
}
}
// call function cleanInput
private static String cleanInput(String in)
{
StringBuilder stringBuilder = new StringBuilder(in);
for(int i = in.length(); i < 8; i++)
{
stringBuilder.insert(1, in.charAt(0));
}
return stringBuilder.toString();
}
// call function add
// adding two binary numbers
public static String add(String byte1, String byte2)
{
if (byte1.charAt(0) == '1')
subtract(byte2, byte1);
if (byte2.charAt(0) == '1')
subtract(byte1, byte2);
StringBuilder sum = new StringBuilder();
if (byte1.length() == 8 && byte2.length() == 8)
{
boolean isRounded = false;
for (int i = 7; i > -1; i--)
{
char bit1 = byte1.charAt(i);
char bit2 = byte2.charAt(i);
if (bit1 == '1' && bit2 == '1')
{
if (isRounded)
sum.insert(0, '1');
else
sum.insert(0, '0');
isRounded = true;
}
else if ((bit1 == '1' || bit2 == '1') && isRounded)
{
sum.insert(0, '0');
isRounded = true;
}
else
{
if (bit1 == '1' || bit2 == '1' || isRounded)
sum.insert(0, '1');
else
sum.insert(0, '0');
isRounded = false;
}
}
}
return sum.toString();
}
// subtract two binay number
public static String subtract(String byte1, String byte2)
{
byte2 = add(byte2.replaceAll("0", "x").replaceAll("1", "0").replaceAll("x", "1"), "00000001");
return add(byte1, byte2);
}
//TODO update this algorithm
public static void byteToDec(String byteString, int dec)
{
if(byteString.length() == 8 && byteString.charAt(0) == '1')
{
System.out.print("-");
byteString = byteString.replaceAll("0", "x").replaceAll("1", "0").replaceAll("x", "1");
dec = 1;
}
if(byteString.length() == 1)
{
int bit = Character.getNumericValue(byteString.charAt(0));
System.out.print(dec + bit * Math.pow(2, 0));
return;
}
int bit = Character.getNumericValue(byteString.charAt(0));
dec += bit * Math.pow(2, byteString.length() - 1);
byteToDec(byteString.substring(1), dec);
}
}
Sample Output
****Menu****
1 -> Add
2 -> Subtract
0 -> quit
>1
Enter 8 bit signed binary number: 00001101
Enter another binary Number> 11111011
00001000(8.0)
****Menu****
1 -> Add
2 -> Subtract
0 -> quit
>2
00001000(8.0)
****Menu****
1 -> Add
2 -> Subtract
0 -> quit
>2
Enter 8 bit signed binary number: 00001101
Enter another binary Number> 11111011
00010010(18.0)
****Menu****
1 -> Add
2 -> Subtract
0 -> quit
>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.