I figured out what was going on, if you case '0' into an int from a char, you can subtract a single digit from it to figure out what it is because of the ASCII codes. Needless to say, I found it out on my own and the teacher had no idea.
Why do you keep trying to sound better than your teacher? First you felt the need to question the legitimacy of the code sample (which ended up being correct), and now you're trying to make it seem like your teacher doesn't even understand their own code. I wouldn't be the least bit surprised if they just couldn't figure out what you were trying to ask them.
Anyway, to give a full description of what the code is doing, you need to understand that char and int8s are one in the same. You don't cast '0' from a char to an int because it already is one. The reason C and C++ have the concept of chars and let you define them using the '0' syntax because it's human-readable. Here's a full and ridiculously tedious description of what the code does (for reference, the code is "d += (bnum.at[i] - '0') * p"):
- first it retrieves the ith character from the character array (which is functionally equivalent to bnum[i] or *(bnum++)).
- keep in mind that this character is actually just an int8 containing the ascii value of that character
- we then subtract the ascii value that matches the character '0' (which is also an int8) and we get another int8 as the result
- now we multiply that int8 by the value in p, where p is most likely an int containing a multiple of 2 (for a binary conversion)
- while it would have been faster to do a simple bit shift, multiplying by p allows the function to be extended to include other number bases <= 10)
- before we run the multiplication, the code most likely automatically casts the int8 into a normal int so that the types match
- finally, we add this int back into d (where d is of course initialized to 0)
If you ever find yourself in a similar situation, where a line of code seems more complex than it needs to be, split the line up into multiple pieces and print out the values at each line. Once you see and understand exactly how the code works, it's a lot easier to determine which parts can be removed. In this case, nothing can be removed without breaking the intended functionality.