从__int64到unsigned int的转换 [英] conversion from __int64 to unsigned int

查看:526
本文介绍了从__int64到unsigned int的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

unsigned int毫秒=(unsigned int)(r& 0xffffffff);


以上语句的含义是什么(这里r是__int64)?请说明

unsigned int milliseconds = (unsigned int)(r & 0xffffffff);


What is the meaning of above statement(here r is __int64)? Please explain

推荐答案

__ int64 r;
unsigned int毫秒_1;
unsigned int毫秒_2;
unsigned int毫秒(3);

r = 0x123456789;
毫秒_1 =(unsigned int)(r&0xffffffff); //#1
毫秒_2 =(unsigned int)r; //#2
毫秒_3 = r; //#3

实际上,如果您在Visual Studio中运行此代码,将可以正常工作而不会出现任何错误或警告.

#1和#2完全相同.
但是#3是隐式用法.某些编译器会抱怨,因为将大变量(64位)值分配给小变量(32位).
__int64 r;
unsigned int milliseconds_1;
unsigned int milliseconds_2;
unsigned int milliseconds_3;

r = 0x123456789;
milliseconds_1 = (unsigned int)(r & 0xffffffff); //#1
milliseconds_2 = (unsigned int)r; //#2
milliseconds_3 = r; //#3

Actually if you run this code in visual studio, will works without any error or warning.

#1 and #2 is exactly same.
But #3 is implicit usage. Some compiler will complain because assign large variable(64 bit) value to small variable(32bit).


它掩盖了64位整数中的32位,仅保留了低32位值并将其分配给32位整数.
It masks off the 32 bits from the 64 bit integer, preserves only lower 32 bits of value and assignes it to a 32 bit integer.


二进制的0xFFFFFFFF仅为32 1s.

如果r__int64,则常量(即整数)将提升为__int64本身,变为0x00000000FFFFFFFF(32个零,后跟32个1).

按位与(&)将产生32个零,后跟r的最右边32个位.

强制转换为(unsigned int)会切掉最左边的32位,并提供可分配给milliseconds的值.

转换不正确地是安全的",因为它遭受有符号和无符号值之间的混合.
(如果r为-1,则毫秒为2 32 -1)

一次投射(milliseconds = (unsigned int)r)将会是相同的.
0xFFFFFFFF in binary are just 32 1s.

if r is an __int64, the constant (that is an integer) is promoted to __int64 itself, becoming 0x00000000FFFFFFFF (32 zeroes followed by 32 1s)

The bitwise AND (&) will result as 32 zeroes followed by the 32 rightmost bits of r.

The cast into (unsigned int) will cut away the 32 leftmost bits, giving a value that is assignable to milliseconds.

The conversion is not properly "safe", since it suffer of the mixing between signed and unsigned values.
(if r is -1, milliseconds will be 232-1)

A one-shot cast ( milliseconds = (unsigned int)r ) would had been the same.


这篇关于从__int64到unsigned int的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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