简体   繁体   English

添加两个二进制字符串

[英]Adding two binary strings

How does code block in while loop execute? while循环中的代码块如何执行?

 string addBinary(string a, string b)
    {
        string s = "";

        int c = 0, i = a.size() - 1, j = b.size() - 1;
        while(i >= 0 || j >= 0 || c == 1)
        {
            c += i >= 0 ? a[i --] - '0' : 0;
            c += j >= 0 ? b[j --] - '0' : 0;
            s = char(c % 2 + '0') + s;
            c /= 2;
        }

        return s;
    }

Also here c is int type, then what does c%2+'0' and any char a[i]-'0' mean?另外这里cint类型,那么c%2+'0'和任何 char a[i]-'0'是什么意思?

tl;dr it's using ASCII arithmetic to translate characters into numbers for use in binary arithmetic. tl; dr 它使用 ASCII 算术将字符转换为用于二进制算术的数字。

Binary addition is the addition of two binary numbers - this code is taking advantage of how C++ casts char to int in order to facilitate this.二进制加法是两个二进制数的加法 - 此代码利用 C++ 将char转换为int的方式来促进这一点。 The ASCII code for a number is how it's represented behind-the-scenes;数字的ASCII 码是它在幕后的表示方式; for example, '0' corresponds to 48 and '1' corresponds to 49. The C++ function '1' - '0' is actually returning 49 - 48 ;例如, '0'对应 48, '1'对应 49。 C++ function '1' - '0'实际上返回49 - 48 it subtracts the ascii code values.它减去ASCII码值。 That's why, for example 'a' - '0' return 49 : the code for 'a' is 97, and 97 - 48 = 49.这就是为什么,例如'a' - '0'返回49'a'的代码是 97,并且 97 - 48 = 49。

a[i] - '0' is an extension of this, taking the character in question and "subtracting" the ASCII value of '0' from it. a[i] - '0'是它的扩展,获取相关字符并从中“减去” '0'的 ASCII 值。 If the character in a[i] is a '0' , this is 48 - 48;如果a[i]中的字符是'0' ,则为 48 - 48; if it's a '1' , it's 49 - 48. The result is a numeral 0 or 1, respectively, which can be used for the binary arithmetic.如果是'1' ,则为 49 - 48。结果分别为数字 0 或 1,可用于二进制算术。

Using the above logic, the code is starting at the right-hand side of the two input strings and parsing the characters one letter at a time, using these properties to determine if the current letter is a '0' or a '1' , and then putting the result into c .使用上述逻辑,代码从两个输入字符串的右侧开始,一次解析一个字母,使用这些属性来确定当前字母是'0'还是'1' ,然后将结果放入c (If you're doing this yourself, you can also cast the character to int as with (int)a[i--] if you want, but this will fail if you give it bad input.) (如果您自己执行此操作,也可以根据需要将字符转换为 int,就像(int)a[i--]一样,但如果输入错误,这将失败。)

The int variable c is receiving a 1 if the current digit in the first string is a '1' and a 0 if it's a '0' .如果第一个字符串中的当前数字是'1' ,则 int 变量c接收 1 ,如果它是'0'则接收 0 。 If both of the characters are '1' s, then c becomes 2;如果两个字符都是'1' ,则c变为 2; this is means we need to "carry" the bit.这意味着我们需要“携带”该位。 The carry is facilitated by c /= 2; c /= 2; : since int s don't round when they're divided, if c is 0 or 1, it will be 0 in the next iteration, whereas if it's 2, it'll be 1 in the next iteration. : 由于int被除法时不取整,所以如果c为 0 或 1,下一次迭代为 0,如果为 2,下一次迭代为 1。

c % s + '0' is converting the number back into a char to be added to the string. c % s + '0'正在将数字转换回要添加到字符串中的char It takes the ASCII code for '0' (48) and either adds 0 to it if c is 0 or 2 (leaving it as 48, or '0' ) or adds 1 to it if c is 1 (changing it to 49, or '1' ).它采用'0' (48) 的 ASCII 码,如果c为 0 或 2(保留为 48 或'0' ),则向其添加 0,或者如果c为 1,则向其添加 1(更改为或'1' )。 As an interesting note, if you add enough to '0' , it'll eventually go into the letters, and then symbols, and some other unprintable ASCII characters.作为一个有趣的注释,如果你添加足够的'0' ,它最终会 go 变成字母,然后是符号,以及其他一些不可打印的 ASCII 字符。

Once you've got the ASCII logic and are ready to tackle the binary logic, this site includes a video explaining the code .一旦您掌握了 ASCII 逻辑并准备好处理二进制逻辑,该站点将包含一个解释代码的视频 Good luck!祝你好运!

Any digit represented as character if you subtract from char '0', you will get its numerical value.如果你从 char '0' 中减去任何表示为字符的数字,你将得到它的数值。 eg '2' -'0' = 2例如'2' -'0' = 2

C%2 - will get you the remainder 0 or 1 and then adding '0' to convert it into character ie from '1' (char) to 1 (integer) C%2 - 将得到余数 0 或 1,然后添加 '0' 将其转换为字符,即从 '1' (char) 到 1 (integer)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM