C ++ - adding or subtracting "0" from a value

I looked at the code below and I got the logic, but I can not understand what '0' .

 class Solution { public: 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; } }; 
+7
c ++
source share
4 answers

C and C ++ standards require the characters '0'..'9' be contiguous and incremented. Therefore, to convert one of these characters to the digit that it represents, you subtract '0' and convert the digit to the character that represents it, add '0' .

+8
source share

C ++ requires ([lex.charset] / 3) that in the base character set the digits '0' , '1' , '2' , ..., '9' are encoded as adjacent values. This means that given the numeric character c you can calculate its integral value as the expression c - '0' .

+4
source share

The value '0' represents the offset of the ascii table to represent a numeric character. To compare two values ​​when one of them is ascii and the other is binary, you need to convert to the same base representation.

+1
source share

In ASCII code, the character 0, represented as "0" in C (and many other languages), has a value of 48. Also in ASCII, the remaining 9 digits are adjacent: "0", "1", etc. A string consists of characters. Therefore, if you subtract "0" by another digit, you will get its numerical value.

+1
source share

All Articles