Question
can anyone help me write this program write a java program to preform twos complement binary addition and subbstraction with 8 bit signed binary numbers.
can anyone help me write this program
"write a java program to preform twos complement binary addition and subbstraction with 8 bit signed binary numbers. you are to write a menu driven program that gives the user three choices, 1) add 2) substract 3) quit. the input to your program will be two signed binary numbers whichh are to be read in as strings. You must write java methods with headers, public static String addBin(String x, String y) and public static String subBin(String x, String y) for addition and substraction. the arithmitic must be preformed in binary and the program should display the input/output numbers in binary as well as in decimal."
I have pasted below a program whith methhods that must be used for this program
package lab3;
import java.util.Scanner;
public class lab3
{
public static void main(String[] args) {
int choice, decimal;
String binary;
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("~~~Select any one from below~~~ " // displays menu
+ "1. Convert decimal to binary " + "2. Convert binary to decimal " + "3. Quit ");
choice = sc.nextInt(); // stores the users input as a choice
if (choice == 1) {
System.out.println(" Enter decimal: ");
decimal = sc.nextInt();
binary = decToBin(decimal); // calls dectobin
System.out.println(" " + decimal + " in binary is " + binary + " ");
} else if (choice == 2) {
System.out.println("Enter binary: ");
sc.nextLine();
binary = sc.nextLine();
decimal = binToDec(binary); // calls bintodec
System.out.println(" " + binary + " in decimal is " + decimal + " ");
} else {
System.out.println(" Program terminated"); // program ends
break;
}
}
sc.close();
}
public static String decToBin(int x) {
String bin = ""; // store the bin string
int rem;
int sign = 1;
if (x < 0) // check if x is negative
{
sign = -1; // change sign
x = -x; // make x positive
}
for (int i = 0; i < 8; i++) { // runs until you get the 8 bit
rem = x % 2; // rem is remainer of the dec / 2
x = x / 2; // x is the dec divided by 2
bin = rem + bin; // the binary number is the remainder and its stored in bin
}
// if negative
if (sign == -1) {
String negBin = ""; // new string
// 1's complement
for (int i = 0; i < bin.length(); i++) {
if (bin.charAt(i) == '0')
negBin = negBin + '1';
else
negBin = negBin + '0';
}
// adding 1
rem = 1;
bin = "";
for (int i = negBin.length() - 1; i >= 0; i--) {
if (rem == 1) {
if (negBin.charAt(i) == '1') {
bin = '0' + bin;
} else {
bin = '1' + bin;
rem = 0;
}
} else {
bin = negBin.charAt(i) + bin;
}
}
}
return bin;
}
public static int binToDec(String s) {
int num = 0;
int sign = 1; // initialize sign
// if negative number
if (s.charAt(0) == '1') {
sign = -1; // change sign
String s1 = ""; // new string
// 1's complement
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == '0')
s1 = s1 + '1';
else
s1 = s1 + '0';
}
// Adding 1
int rem = 1;
s = "";
for (int i = s1.length() - 1; i >= 0; i--) {
if (rem == 1) {
if (s1.charAt(i) == '1') {
s = '0' + s;
} else {
s = '1' + s;
rem = 0;
}
} else {
s = s + s1.charAt(i);
}
}
}
for (int i = 7; i >= 0; i--) // i is the power
if (s.charAt(i) == '1') // if 1 appears, it takes 2^(7-i)
num = num + (int) Math.pow(2, 7 - i);
return sign * num;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started