HELLO I WANT TO ASK WHAT THIS STATEMENT WOULD MEAN IN C++
string ans = "11";
ans += ans.back() ^ '0' ^ '1';
cout << ans << '\n';
I WAS WONDERING HOW CAN WE XOR CHARACTERS ??
PLEASE HELP
HELLO I WANT TO ASK WHAT THIS STATEMENT WOULD MEAN IN C++
string ans = "11";
ans += ans.back() ^ '0' ^ '1';
cout << ans << '\n';
I WAS WONDERING HOW CAN WE XOR CHARACTERS ??
PLEASE HELP
When asked to xor a char with an int, the compiler will “promote” that char to also be of int type, so we have:
ans += ans.back() ^ '0' ^ '1';
which is equivalent to:
ans += '1' ^ '0' ^ '1';
which in turn is equivalent to (after promoting each char to its corresponding int value - see the ASCII table for more details):
ans += 49 ^ 48 ^ 49;
i.e.
ans += 48;
As to how we can append this 48 (which is an int) to ans (which is a std::string), it uses this overload (see here):
basic_string& operator+=( CharT ch );
with CharT == char, so it just re-casts the int value 48 back to its char value ('0') and so appends the char '0' to ans, leaving ans with the final value "110".
TYSM Brother … you always help quickly…