gpt4 book ai didi

java - 添加二进制数

转载 作者:太空狗 更新时间:2023-10-29 22:41:48 25 4
gpt4 key购买 nike

有谁知道如何在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com