Velocity Reviews > C++ > Re: static_cast question

# Re: static_cast question

Alan Sung
Guest
Posts: n/a

 08-02-2003
"Russell Hanneken" <(E-Mail Removed)> wrote in message
news:HWBWa.1531\$(E-Mail Removed) k.net...
> Aaron Anodide wrote:
> > Is there any different in the result of the following two lines of code?
> >
> > LONGLONG l;
> >
> > DWORD d = static_cast<DWORD>(l) / 30;
> >
> > DWORD d2 = static_cast<DWORD>(l/30);

>
> Possibly. For example, suppose that LONGLONG is an integer type, and
> DWORD is a floating point type. In the first case, the left operand of
> the division operation would be a floating point number, so the division
> would be floating point division. In the second case, the left operand
> of the division operation would be an integer, so the division would be
> integer division; any fractional part of the result would be
> discarded. So if l is 36, d would be initialized with 1.2, while d2
> would be initialized with 1.0.
>
> But I have no idea what DWORD and LONGLONG are in the context of
> whatever program you're looking at, so I don't really know if there's a
> difference.
>
> Regards,
>
> Russell Hanneken
> http://www.velocityreviews.com/forums/(E-Mail Removed)

This is obviously coming from the Windows world.
DWORD is unsigned long (32-bit)
LONGLONG is a signed 64-bit

> DWORD d = static_cast<DWORD>(l) / 30;

Basically throw away the high-order 32-bits of "ell", do an integer divide
by 30 (32-bit)

> DWORD d2 = static_cast<DWORD>(l/30);

Promote the constant 30 to a 64-bit value, do an integer divide of "ell" by
this, then throw away the high-order 32-bits. Might be clearer if you wrote
(on Windows with a 64-bit compiler):

DWORD d2 = static_cast<DWORD>(l/30Ui64);

Definitely different results for large values of "ell". ("ell"/30) may fit
into 32-bits, but "ell" may not.

-al sung
Rapid Realm Technology, Inc.
Hopkinton, MA