Question: Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result
Implement a Java method named addBinary() that takes two String arguments (each representing a binary value) and returns a new String corresponding to the result of performing binary addition on those arguments. Before you begin, if one of the arguments is shorter than the other, call your pad() method from the previous step to extend it to the desired length.
Note: ped() method is
public static String pad(String input, int size) { if(input.length()>=size) { return input; } String a = ""; for(int i=0; i < (size - input.length()); i++ ) a += "0"; return (a + input); }
Define a Java method named toTwosComplement() that takes two integer arguments: a base 10 value to be translated into twos complement representation, and a second integer representing the length of the twos complement representation. The method converts the first argument into a String (containing only 1s and 0s) of the specified length and returns that String.
Use the following algorithm to convert an integer value into twos complement form:
a. If the integer value is positive or zero:
i. Translate the integer value into a binary string using repeated division by 2 (i.e., using the algorithm you discussed in class).
ii. Use pad() to extend the binary string to the required length.
b. Otherwise (meaning that the integer value is negative):
i. Translate the absolute value of the integer into a binary string (for example, given the integer value -6, translate 6 into a binary string).
ii. Use pad() to extend the binary string to the required length.
iii. Negate the binary string by inverting its value and adding1 (using binary addition):
1. To invert a binary string, create a new String of the same length where all the 1s have been replaced by 0s and all the 0s have been replaced by 1s. For example, inverting 0010101 would produce the string 1101010.
2. Add the binary value 1 to the inverted string from the previous step using your binaryAdd() method.
iv. If the negated binary string is greater than the specified length (due to an extra carry bit), remove the leading character (bit). For example, suppose that we wish to convert -13 to 6-bit twos complement representation (meaning that our method call is toTwosComplement(-13, 6)). Positive 13 in binary is 1101; padding this to a length of 6 gives us 001101. Since the original number was negative, we invert the bits of the padded binary representation to get 110010. Adding 1 with binary addition gives us a final value of 110011 (if we had exceeded 6 bits, we would have returned only the last 6 bits of the result).
4. Finally, write a small Java program that tests your methods via two tasks: a. Read in two binary strings from the user, add them using binary addition, and print the result. For example, 110 + 1101 = 10011. Likewise, 1001 + 1111 = 11000. b. Read in a decimal (base 10) integer value from the user and a desired length in bits. Your program should calculate and display the twos complement representation of the original integer value. For example, 34 in 8-bit twos complement is 00100010, while -305 in 10-bit twos complement is 1011001111.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
