Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: static_cast question

Reply
Thread Tools

Re: static_cast question

 
 
Alan Sung
Guest
Posts: n/a
 
      08-02-2003
"Russell Hanneken" <> wrote in message
news:HWBWa.1531$ 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
>


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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner's question on static_cast and const_cast subramanian100in@yahoo.com, India C++ 4 11-14-2007 04:27 PM
Is static_cast<int>(static_cast<double>(a)) == a? Bo Peng C++ 11 10-20-2006 12:59 PM
static_cast<unsigned int> question... GuineaPig C++ 9 11-15-2003 12:02 PM
Re: static_cast question John Harrison C++ 1 08-03-2003 05:54 PM
static_cast confusion Chandra Shekhar Kumar C++ 7 06-27-2003 02:54 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57