c – 无符号长龙超出范围?

前端之家收集整理的这篇文章主要介绍了c – 无符号长龙超出范围?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
好的,这是一个奇怪的问题:

>我使用无符号长整型变量(我使用甚至更长的变量,具有相同的效果)
>我需要能够存储64位整数(sizeof返回8,这很好)

但是,当我尝试转换为1< 63的值,并执行一些简单的按位操作时,我奇怪地似乎正在获得负值.为什么? 我的测试代码

unsigned long long c = 0;

    c |= 1l << 56; printf("c = %lld\n",c);
    c |= 1l << 63; printf("c = %lld\n",c);

输出

c = 72057594037927936 
c = -9151314442816847872

Sidenotes:

>当然,即使我直接做c = 1l< <63,也会发生同样的事情.
>在Mac OS X 10.6上进行的所有测试,并使用Apple的LLVM Compiler 3.0进行编译

有什么建议么?

@R_502_323@

%lld说明符的d部分告诉printf该参数应被视为有符号整数.使用u代替:%llu.

从手册页:

d,i

The int argument is converted to signed decimal notation.


o,u,x,x

The unsigned int argument is converted to unsigned octal (o),unsigned decimal (u),or unsigned hexadecimal (x and X) notation.

原文链接:https://www.f2er.com/c/113699.html

猜你在找的C&C++相关文章