如何添加二进制数 [英] how to add binary numbers

查看:151
本文介绍了如何添加二进制数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在这里进行方法.一个将十进制数转换为8位二进制数.我需要添加另一个将两个二进制数加在一起的方法.例如01000000(64)+ 00000010(2)= 01000010(66).然后,对于第二种方法,例如34 + 56等于90,然后是等效的01011010.因此,我需要一种执行该方法的方法.因此,如果要求用户输入两个十进制数字,则将两个数字相加并相加.将答案以二进制(8位)的格式显示

I have to methods here. One converts decimal number to 8 bit binary. I need to add another method that will add two binary number together. For example 01000000 (64) + 00000010(2) = 01000010(66). Then for the second method for example 34 + 56 equal 90 then the binary equivalent which 01011010. So I would need a method that performs that. So if the user was asked for two decimal numbers it would take the two and add them. Out putting the answer in binary (8 bits)

这会将十进制转换为二进制,我需要一些如何将两个二进制数字相加

This converts a decimal to binary, I need to some how get it to add two binary numbers

public String toBinary(int number)
{
    String binary = " ";
    boolean isNeg = number < 0;
    if(isNeg)
    {
        number += 1;
    }
    while (number > 0 || number < 0) {
        int rem = number%2; 
        if(rem == -1)
        {
            rem = rem * -1;
        }
        binary = rem + binary;
        number /= 2; 

    } 
    while(binary.length() <= 8)
    {
        binary = "0" + binary;
    }

    if(isNeg)
    {
        StringBuffer buff = new StringBuffer("");
        for(int i =0; i < 8; i++)
        {
            char store = binary.charAt(i);  
            if(store == '0')
            {
                buff.append("1");

            }else
            {
                buff.append("0");
            }

        }  

        binary = buff.toString();
    }     

    return binary;
}

将二进制转换为十进制,我需要对其进行修复以使其适用于负二进制(单数),然后能够添加两个二进制数字

converts binary to decimal i need to fix it to work for negative binary (singed) then be able to add two binary numbers

public int toDecimal(int number)
{
    int decimal = 0;
    int p = 0;
    while(true)
    {
        if(number ==0)
        {
            break;
        }else if(number > 0) {
            int temp = number%10;
            decimal +=temp*Math.pow(2,p);
            number = number/10;
            p++;
        }



    }
    return decimal;

}}

推荐答案

首先,很遗憾地告诉您,但是...这两种方法已经存在,但是如果您尝试对它们进行编码,那就太好了

First, sorry to tell you that but ... both methods already exists, but great if you tried to code them

//The method to convert from int to Binary already exists : Integer.toBinaryString(int i)
System.out.println(toBinary(16)+" <=> "+Integer.toBinaryString(16));
//The method to convert from Binary to int (with String) too : Integer.parseInt(String s,2)
System.out.println(toDecimal(00010000)+" <=> "+Integer.parseInt(String.valueOf(10000),2));

对于'add'方法,只需使用其他2种方法:

For the 'add' method just use your 2 other methods :

public String addBinary(int a, int b){
        return toBinary(toDecimal(a)+toDecimal(b));
}

对您来说,十进制:永远不要使用while(true)这是非常危险的,并且在循环外实例化变量(此处为else),因为您每次都在创建一个新变量:使用内存

For you toDecimal : NEVER USE while(true) it's very risky, and instantiate your variable outside of the loop (here the else if) because your are creating a new one each time : use memory

这篇关于如何添加二进制数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆