Split a long integer into eight 4-bit values It's an exercise from a book. There is a given long integer that I should convert into 8 4-bit  values. The exercise is not very clear for me, but I think I understood the task correctly. 

I googled how to return with an array in a function. In the splitter function I add the last 4 bits into the values array, then cut the them from the original number. 

Is this a good way to do this?

#include &lt;iostream&gt;

int * splitter(long int number)
{
    static int values[8];

    for (int i = 0; i &lt; 8; i++)
    {
        values[i] = (int)(number &amp; 0xF);
        number = (number &gt;&gt; 4);
    }

    return values;
}

int main()
{
    long int number = 432214123;
    int *values;
    values = splitter(number);
    for (int i = 7; i &gt;= 0; i--)
        std::cout &lt;&lt; values[i] &lt;&lt; " ";
    return 0;
}
There are only three issues I take with your code...


The caller of the splitter function has the possibility of trying to access a value outside of the returned array and could cause a memory access violation if they did so. For this reason alone, it would be better to go with the vector approach that @Jamal posted. 
You should use the C++ style casting (e.g. static_cast&lt;int&gt;(number &amp; 0XF)) instead of the C style casting (e.g. (int)(number &amp; 0xF)). 
You should use std::int32_t instead of long or std::uint32_t instead of unsigned long. long is not gauranteed to be 32 bits, especially on 64 bit platforms. I don't take as big of an issue with this as some do though because on most systems, it will be at least 32 bits. 


Here is a solution that only returns one nybble at a time and throws an exception if the caller tries to access an invalid portion of the of number. Since your book hasn't talked about pointers or vectors yet, this might actually be the type of approach it is looking for (possibly minus the exception throwing if it hasn't talked about exceptions yet either). If you take away the exception throwing, the caller still cannot cause a memory access violation though. If they passed something lower than 0 or greater than 7 as the value of part they would just get a return value of 0. 

#include &lt;iostream&gt;
#include &lt;stdexcept&gt;
#include &lt;cstdint&gt;

unsigned short get_nybble( std::uint32_t number, const unsigned short part )
{
    if( part &gt; 7 )
        throw std::out_of_range("'part' must be a number between 0 and 7");
    return (number &gt;&gt; (4 * part)) &amp; 0xF;
}

int main( int argc, char* argv[] )
{
    std::uint32_t number = 432214123;
    try {
        for( short i = 7; i &gt;= 0; i-- )
            std::cout &lt;&lt; get_nybble(number, i) &lt;&lt; " ";
    } catch( std::exception&amp; ex ) {
        std::cerr &lt;&lt; "Error: " &lt;&lt; ex.what() &lt;&lt; std::endl;
    }
    return 0;
}


This may not be the best solution, but another approach I find interesting is an anonymous union combined with a bit field struct. This works with the GNU G++ compiler and should also work with Visual C++. Note that as I understand it this is potentially unsafe on non x86 systems due to the structure packing which I'd bet your book has not talked about yet either. 

#include &lt;iostream&gt;
#include &lt;cstdint&gt;

#pragma pack(push, 1)
typedef union 
{
    std::uint32_t value;
    struct 
    {
        std::uint32_t part1: 4;
        std::uint32_t part2: 4;
        std::uint32_t part3: 4;
        std::uint32_t part4: 4;
        std::uint32_t part5: 4;
        std::uint32_t part6: 4;
        std::uint32_t part7: 4;
        std::uint32_t part8: 4;
    };
} nybbles;
#pragma pack(pop)

int main( int argc, char* argv[] )
{
    nybbles num;
    num.value = 432214123;
    std::cout &lt;&lt; "Parts = "
        &lt;&lt; num.part8 &lt;&lt; ", "
        &lt;&lt; num.part7 &lt;&lt; ", "
        &lt;&lt; num.part6 &lt;&lt; ", "
        &lt;&lt; num.part5 &lt;&lt; ", "
        &lt;&lt; num.part4 &lt;&lt; ", "
        &lt;&lt; num.part3 &lt;&lt; ", "
        &lt;&lt; num.part2 &lt;&lt; ", "
        &lt;&lt; num.part1 &lt;&lt; std::endl;
    return 0;
}