无符号长,带负值 [英] Unsigned long with negative value

查看:128
本文介绍了无符号长,带负值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅下面的简单代码:

  #include< iostream> 
#include< stdlib.h>

using namespace std;


int main(void)
{
unsigned long currentTrafficTypeValueDec;
long input;
input = 63;
currentTrafficTypeValueDec =(unsigned long)1LL<输入;
cout<< currentTrafficTypeValueDec<< endl;
printf(%u \\\
,currentTrafficTypeValueDec);
printf(%ld \\\
,currentTrafficTypeValueDec);
return 0;为什么printf()显示具有负值的currentTrafficTypeValueDec(unsigned long)?



输出为:

  9223372036854775808 
0
-92233720368547758


解决方案

/ p>

cout 将数字打印为Unsigned Long,所有64位都有效,并打印为无符号二进制整数认为此处的格式为%lu )。



printf 。将输入视为正常的无符号整数(32位?),这会导致位33到64下降 - 留下零。



printf(%ld ... 将输入视为64位有符号数字,并只打印输出。



你可能会发现最后一个 printf 令人困惑的是,它给出了与 cout 相同的绝对值,当以无符号整数查看时,所有64位在产生整数值时是重要的。然而,对于有符号数字,位64是符号位。当符号位置位时(如在示例中),它指示剩余的63位将被视为 2的赞美。正数只是通过将其二进制值转换为十进制来打印。然而,对于负数,发生以下情况:打印负号,用二进制1位将XOR位1至63加1,将结果加1并打印无符号值。通过丢弃符号位(位64),结束63'0'位,与'1'位异或为63'1'位,加+1,整个事情翻转给你一个无符号整数有位64设置为1,其余设置为0 - 这与您在 cout 中得到的相同。



一旦你找出了为什么上面的解释是正确的,你也应该能够从


Please see the simple code below:

#include <iostream>
#include <stdlib.h>

using namespace std;


int main(void)
{
    unsigned long currentTrafficTypeValueDec;
    long input;
    input=63;
    currentTrafficTypeValueDec = (unsigned long) 1LL << input; 
    cout << currentTrafficTypeValueDec << endl;
    printf("%u \n", currentTrafficTypeValueDec);
    printf("%ld \n", currentTrafficTypeValueDec);
    return 0;
}

Why printf() displays the currentTrafficTypeValueDec (unsigned long) with negative value?

The output is:

9223372036854775808
0
-9223372036854775808 

解决方案

Fun with bits...

cout is printing the number as an Unsigned Long, all 64 bits are significant and print as unsigned binary integer (I think the format here would be %lu).

printf(%u ... treats the input as an normal unsigned integer (32 bits?). This causes bits 33 through 64 to drop off - leaving zero.

printf(%ld ... treats the input as a 64 bit signed number and just prints it out as such.

The thing you might find confusing about the last printf is that it gives the same absolute value as cout, but with a minus sign. When viewing as an unsigned integer all 64 bits are significant in producing the integer value. However for signed numbers, bit 64 is the sign bit. When the sign bit is set (as it is in your example) it indicates the remaining 63 bits are to be treated as a negative number represented in 2's compliment. Positive numbers are printed simply by converting their binary value to decimal. However for a negative number the following happens: Print a negative sign, XOR bits 1 through 63 with binary '1' bits, add 1 to the result and print the unsigned value. By dropping the sign bit (bit 64) you end up with 63 '0' bits, XORing with '1' bits gives you 63 '1' bits, add +1 and the whole thing rolls over to give you an unsigned integer having bit 64 set to '1' and the rest set to '0' - which is the same thing you got with cout BUT, as a negative number.

Once you have worked out why the above explanation is correct you should also be able to make sense out of this

这篇关于无符号长,带负值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆