Re: static_cast question

Discussion in 'C++' started by Alan Sung, Aug 2, 2003.

  1. Alan Sung

    Alan Sung Guest

    "Russell Hanneken" <> wrote in message
    news:HWBWa.1531$...
    > 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
    Alan Sung, Aug 2, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Harrison

    Re: static_cast question

    John Harrison, Aug 3, 2003, in forum: C++
    Replies:
    1
    Views:
    651
    Aaron Anodide
    Aug 3, 2003
  2. GuineaPig
    Replies:
    9
    Views:
    546
    Fraser Ross
    Nov 15, 2003
  3. Bo Peng
    Replies:
    11
    Views:
    1,059
    Victor Bazarov
    Oct 20, 2006
  4. , India
    Replies:
    4
    Views:
    514
    red floyd
    Nov 14, 2007
  5. junyangzou
    Replies:
    13
    Views:
    244
Loading...

Share This Page