Hello Jianshu

2019-02-14  本文已影响0人  less__is__more

print the binary data

C++

template <typename T>
void printb(const T& t)
{
    const size_t len = sizeof(T) * 8;
    char s[len + 1];
    typedef unsigned long long ull;
    auto buff = const_cast<ull&>(reinterpret_cast<const ull&>(t));

    s[len] = '\0';
    for (size_t i = len - 1; i != (size_t)-1; --i)
    {
        s[i] = buff & 1 ? '1' : '0';
        buff >>= 1;
    }
    
    cout << s;
}
上一篇下一篇

猜你喜欢

热点阅读