java - 添加二进制数

标签 java binary decimal addition

有谁知道如何在 Java 中将以二进制形式输入的 2 个二进制数相加?

例如,1010 + 10 = 1100

最佳答案

使用Integer.parseInt(String, int radix) .

 public static String addBinary(){
 // The two input Strings, containing the binary representation of the two values:
    String input0 = "1010";
    String input1 = "10";

    // Use as radix 2 because it's binary    
    int number0 = Integer.parseInt(input0, 2);
    int number1 = Integer.parseInt(input1, 2);

    int sum = number0 + number1;
    return Integer.toBinaryString(sum); //returns the answer as a binary value;
}

关于java - 添加二进制数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8548586/

相关文章:

java - 如何以编程方式为 ElasticSpaceDeployment 配置 GigaSpaces XAP 持久性?

java - 什么是 C/C++ 等同于 Java 的异步任务

java - 将 JSON 映射到 Java 对象的正确方法

c# - 谷歌货币转换器

将文件内容转换为十六进制格式

Excel 打印带有正确小数分隔符的数字

java - SQLiteBlobTooBigException : Row too big to fit into CursorWindow while writing to DB

java - 相当于00000000字节的字符是什么?

c++ - 十进制转二进制(反之亦然)

支持二进制数据的javascript压缩算法?