#include <iostream>
#include <iomanip>
#include <string>
// returns '1' for true, '0' for false.
char exclusive_or(char a, char b)
{
return (a != b) + '0' ;
}
std::string to_gray(const std::string& binary)
{
std::string result(1, binary[0]);
result.reserve(binary.size() + 1);
for (unsigned i = 1; i < binary.size(); ++i)
result += exclusive_or(binary[i - 1], binary[i]);
return result;
}
int main()
{
std::string number;
std::cout << std::right ;
std::cout << " bin = gray\n" ;
while ( std::getline(std::cin, number) && !number.empty() )
{
std::cout << std::setw(4) << number ;
std::cout << " = " ;
std::cout << std::setw(4) << to_gray(number) << '\n' ;
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8aW9tYW5pcD4KI2luY2x1ZGUgPHN0cmluZz4KCi8vIHJldHVybnMgJzEnIGZvciB0cnVlLCAnMCcgZm9yIGZhbHNlLgpjaGFyIGV4Y2x1c2l2ZV9vcihjaGFyIGEsIGNoYXIgYikKewogICAgcmV0dXJuIChhICE9IGIpICsgJzAnIDsKfQoKc3RkOjpzdHJpbmcgdG9fZ3JheShjb25zdCBzdGQ6OnN0cmluZyYgYmluYXJ5KQp7CiAgICBzdGQ6OnN0cmluZyByZXN1bHQoMSwgYmluYXJ5WzBdKTsKICAgIHJlc3VsdC5yZXNlcnZlKGJpbmFyeS5zaXplKCkgKyAxKTsKCiAgICBmb3IgKHVuc2lnbmVkIGkgPSAxOyBpIDwgYmluYXJ5LnNpemUoKTsgKytpKQogICAgICAgIHJlc3VsdCArPSBleGNsdXNpdmVfb3IoYmluYXJ5W2kgLSAxXSwgYmluYXJ5W2ldKTsKCiAgICByZXR1cm4gcmVzdWx0Owp9CgppbnQgbWFpbigpCnsKICAgIHN0ZDo6c3RyaW5nIG51bWJlcjsKICAgIAogICAgc3RkOjpjb3V0IDw8IHN0ZDo6cmlnaHQgOwogICAgCiAgICBzdGQ6OmNvdXQgPDwgIiBiaW4gPSBncmF5XG4iIDsKICAgIHdoaWxlICggc3RkOjpnZXRsaW5lKHN0ZDo6Y2luLCBudW1iZXIpICYmICFudW1iZXIuZW1wdHkoKSApCiAgICB7CiAgICAJc3RkOjpjb3V0IDw8IHN0ZDo6c2V0dyg0KSA8PCBudW1iZXIgOwogICAgCXN0ZDo6Y291dCA8PCAiID0gIiA7CiAgICAJc3RkOjpjb3V0IDw8IHN0ZDo6c2V0dyg0KSA8PCB0b19ncmF5KG51bWJlcikgPDwgJ1xuJyA7CiAgICB9Cn0=