#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>
#include <string>
using value_type = unsigned long long;
std::string as_binary(value_type value)
{
std::ostringstream os;
if (value == 0) // no bits set
os << value;
else
{
// set mask's left-most bit.
value_type mask =
value_type{1} << (std::numeric_limits<value_type>::digits - 1);
while ((mask & value) == 0) // skip leading 0 bits
mask >>= 1; // shift our set bit to the right
while (mask)
{
os << ((mask & value) ? 1 : 0);
mask >>= 1;
}
}
return os.str();
}
int main()
{
for (value_type i = 0; i < 33; ++i)
{
std::cout << std::setw(2) << i;
std::cout << " = ";
std::cout << std::setw(6) << as_binary(i) << '\n';
}
}
I2luY2x1ZGUgPGlvbWFuaXA+CiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGxpbWl0cz4KI2luY2x1ZGUgPHNzdHJlYW0+CiNpbmNsdWRlIDxzdHJpbmc+Cgp1c2luZyB2YWx1ZV90eXBlID0gdW5zaWduZWQgbG9uZyBsb25nOwoKc3RkOjpzdHJpbmcgYXNfYmluYXJ5KHZhbHVlX3R5cGUgdmFsdWUpCnsKICAgIHN0ZDo6b3N0cmluZ3N0cmVhbSBvczsKCiAgICBpZiAodmFsdWUgPT0gMCkgICAgICAgICAgICAgICAgICAgICAgICAgLy8gbm8gYml0cyBzZXQKICAgICAgICBvcyA8PCB2YWx1ZTsKICAgIGVsc2UKICAgIHsKICAgICAgICAvLyBzZXQgbWFzaydzIGxlZnQtbW9zdCBiaXQuCiAgICAgICAgdmFsdWVfdHlwZSBtYXNrID0gCiAgICAgICAgICAgIHZhbHVlX3R5cGV7MX0gPDwgKHN0ZDo6bnVtZXJpY19saW1pdHM8dmFsdWVfdHlwZT46OmRpZ2l0cyAtIDEpOwoKICAgICAgICB3aGlsZSAoKG1hc2sgJiB2YWx1ZSkgPT0gMCkgICAgICAgICAvLyBza2lwIGxlYWRpbmcgMCBiaXRzCiAgICAgICAgICAgIG1hc2sgPj49IDE7ICAgICAgICAgICAgICAgICAgICAgLy8gc2hpZnQgb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