小编Raf*_*ski的帖子

打印长整数位(C++)

我想打印一个很长的数字的所有位.当我在main()中执行它时一切都很好,但在printBits()函数(代码相同)中,第32位有额外的1.

代码:

#include <iostream>

void printBits(long long number)
{
    std::cout<<number<<" -> ";
    for (char i=63; i>=0; --i)
    {
        std::cout<<(bool)(number&(1<<i));
    }
    std::cout<<std::endl;
}

int main()
{
    long long number=1;

    std::cout<<number<<" -> ";
    for (char i=63; i>=0; --i)
    {
        std::cout<<(bool)(number&(1<<i));
    }
    std::cout<<std::endl;

    printBits(number);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

结果是:

1 -> 0000000000000000000000000000000000000000000000000000000000000001
1 -> 0000000000000000000000000000000100000000000000000000000000000001

Process returned 0 (0x0)   execution time : 0.012 s
Press any key to continue.
Run Code Online (Sandbox Code Playgroud)

c++ bits long-long

8
推荐指数
1
解决办法
181
查看次数

标签 统计

bits ×1

c++ ×1

long-long ×1