#include <iostream>
#include <string>
#include <limits>
#include <type_traits>


template <typename int_type,
          class = typename std::enable_if<std::is_unsigned<int_type>::value>::type>
std::string to_binary(int_type num)
{
    if (num == 0)
        return std::string(1, '0');

    // begin with most significant bit:
    int_type mask = 1 << (std::numeric_limits<int_type>::digits - 1);

    while (mask && !(mask&num)) // skip over leading 0s
        mask >>= 1;

    std::string result;
    while (mask)
    {
        result += (mask & num) ? '1' : '0';
        mask >>= 1;
    }

    return result;
}

template <typename int_type,
          class = typename std::enable_if<std::is_unsigned<int_type>::value>::type>
unsigned groups_of_1s(int_type num)
{
    int_type mask = 1;
    bool in_group = false;
    unsigned count = 0;

    while (mask)
    {
        if (mask & num)
        {
            if (!in_group)
            {
                in_group = true;
                ++count;
            }
        }
        else
            in_group = false;

        mask <<= 1;
    }

    return count;
}

int main()
{
    const unsigned value = 183;
    std::cout << value << ": " << to_binary(value) << ", ";
    std::cout << groups_of_1s(value) << " groups of 1.\n";
}