这是我的代码:
template <typename T> void printlnbits(T v) {
const int value_size = sizeof(v) * 8;
long long int* ch = (long long int*)&v;
int j = 0;
for (int i = value_size - 1; i >= 0; --i)
{
extractBit(*ch, i);
j++;
if (j == 8)
{
std::cout << " ";
j = 0;
}
}
std::cout << "\t" << value_size << std::endl;
}
和
void extractBit(long long int ch, int ith) {
std::cout << (ch & (1 << ith) ? 1 : 0);
}
传入参数时:
const long long unsigned e = 1LLU << 40;
输出为:
10000000 00000000 00000000 00000000 10000000 00000000 00000000 00000000 64
现在 1LLU << 40 = 1099511627776
输出不应该是:
00000000 00000000 00000000 10000000 00000000 00000000 00000000 00000000 64
我错过了什么吗?
结构作为参数:
当我将结构作为参数发送时,结构内变量的计算如何一起计算并存储一个值,然后将其作为位表示输出?
struct foo {
int a = 2;
char b = -1;
unsigned long long int x = 1LLU << 63;
};
输出:
00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11001100 11001100 11001100 11111111 00000000 00000000 00000000 00000010 128