Click here to Skip to main content
15,886,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
unsigned int milliseconds = (unsigned int)(r & 0xffffffff);


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

__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).
 
Share this answer
 
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.
 
Share this answer
 
v2
Comments
project.gearup 14-Sep-10 9:18am    
can you please elaborate your answer? cos lower 32 means interms of place or value?
HimanshuJoshi 14-Sep-10 9:21am    
In terms of value
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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900