使用Java中的带符号二进制数执行二进制加法和减法

时间:2016-02-08 02:53:19

标签: java binary

为了享受纯粹的享受和我的时间,我正在努力学习二进制数和装配的更多信息。我去过当地的图书馆检查了一些书籍,以便更好地理解二进制数和加法。几个星期前我甚至都不知道,可以用Java添加二进制数。我被绊倒,已经两天了。

就像我说的那样,我试图在加法和减法中添加二进制数。我假设我需要解析所有内容才能使其正常工作。我觉得我在这里找东西。究竟缺少了什么?任何帮助都是有帮助的。

截至目前,我有这个:

`import java.util.Scanner;
public class binary operation {
public static void main(String[]args){
    Scanner keyboard = new Scanner(System.in);
    boolean keepGoing = true;
    String binary1, binary2;
    int num1, num2;

    System.out.println("Scenario: Choose (A)dd, (S)ubtract, (E)xit");
    char choice = keyboard.next().charAt(0);
    while(keepGoing){
        if (choice == 'A' || choice == 'a'){
            System.out.println("Enter 8-bit signed binary number: "); 
            binary1 = keyboard.next();
            System.out.println("Enter another binary number: ");
            binary2 = keyboard.next();
            num1 = Integer.parseInt(binary1, 2);
            num2 = Integer.parseInt(binary2, 2);
            int sum = num1 + num2;
            System.out.println(Integer.toBinaryString(sum));
        }
        else if(choice == 'S' || choice == 's'){
            System.out.println("Enter 8-bit signed binary number: ");
            binary1 = keyboard.next();
            System.out.println("Enter another binary number: ");
            binary2 = keyboard.next();
            num1 = Integer.parseInt(binary1, 4);
            num2 = Integer.parseInt(binary2, 4);
            int difference = num1 - num2;
            System.out.println(Integer.toBinaryString(difference));
        }
        else if(choice == 'E' || choice == 'e'){
            System.out.println("Thank you.");
            keepGoing = false;
            System.exit(0);
        }
        if(keepGoing){
            System.out.println("Choose (A)dd, (S)ubtract, (E)xit");
            choice = keyboard.next().charAt(0);
        }
    }

1 个答案:

答案 0 :(得分:0)

android:minHeight="48dip"始终打印为32位无符号值。你需要一个类似的方法。

function area = traparea(a,b,h)
%  traparea(a,b,h)   Computes the area of a trapezoid given
%                    the dimensions a, b and h, where a and b
%                    are the lengths of the parallel sides and
%                    h is the distance between these sides

%  Compute the area, but suppress printing of the result
area = 0.5*(a+b)*h;