Question: at the moment I have script which prints out numeric values into bits so for example 

```
print((short) 1);

```

I get a value of `00000001`, but how can I get for this a value like `00000001 00000000` and in case if I print `print((int) 1);` I get a value of `00000001 00000000 00000000 00000000`.
Here is my code:

```
void printbyte(unsigned char x)
{
    for (int i = 0; i < 8; i++)
    {
        if (x & 0x80) cout << 1;
        else cout << 0;
        x = x << 1;
    }
    cout << endl;
}

template <typename T>
void print (T A)
{
    unsigned char *p = (unsigned char *) &A;
    printbyte(*p);
}

int main()
{
    print((short) 1);

    system("pause");
    return 0;
}

```

Answer: 
You can just use `sizeof(T)` within `print` to determine how many bytes to process and then call `printbyte` for each byte, e.g.

```
#include <iostream>
#include <climits>

using namespace std;

void printbyte(unsigned char x)
{
    for (int i = 0; i < CHAR_BIT; i++)
    {
        cout << ((x & 0x80) != 0);
        x <<= 1;
    }
}

template <typename T>
void print (T A)
{    
    for (size_t i = 0; i < sizeof(T); ++i)
    {
        unsigned char b = A >> ((sizeof(T) - i - 1) * CHAR_BIT);
        //unsigned char b = A >> (i * CHAR_BIT); // use this for little endian output
        printbyte(b);
        cout << " ";   
    }
    cout << endl;
}

int main()
{
    print((short) 1);
    print((long long) 42);

    return 0;
}

```
I think you should use [std::bitset](http://www.cplusplus.com/reference/bitset/bitset/), but you always have to specify the size at compile time.
To avoid that, you can use a template function like this (note that this is C++11):

```
#include <iostream>
#include <bitset>
#include <limits>

template <class T> void PrintBits (const T &Number) {
    std::bitset<std::numeric_limits<T>::digits> Bit_Number(Number);
    std::cout << Bit_Number << std::endl;

}

```