static_cast<unsigned>

Discussion in 'C++' started by bonham, Jun 8, 2005.

  1. bonham

    bonham Guest

    i m a beginner in C++. I have some questions:
    what is static_cast<unsigned>?
    i saw someone use static_cast<unsigned> and static_cast<unsigned char>
    together. why?
    e.g. static_cast<unsigned>(static_cast<unsigned char>(c))
    (c is a char).

    Thank you!

    Bon
    bonham, Jun 8, 2005
    #1
    1. Advertising

  2. bonham

    Me Guest

    > i m a beginner in C++. I have some questions:
    > what is static_cast<unsigned>?
    > i saw someone use static_cast<unsigned> and static_cast<unsigned char>
    > together. why?
    > e.g. static_cast<unsigned>(static_cast<unsigned char>(c))
    > (c is a char).


    Take an implementation where char ranges from -128 to 127, the cast
    from char to unsigned char (on this implementation) converts it to the
    range 0 to 255 and then the cast to unsigned int leaves it in that 0 to
    255 range. Without the unsigned char cast there, converting the char to
    an unsigned integer directly (lets say unsigned int ranges from 0 to
    0xFFFF on this implementation) would convert it to either 0 to 127 if c
    is positive or 0xFFFF+1-128 to 0xFFFF+1-1 if c is negative (all these
    ranges here are inclusive).

    So basically, it's because char isn't guaranteed to be signed or
    unsigned, so we want to force it to become unsigned.

    If the code is doing what I think it's trying to do, it's wrong in
    general (but works on the majority of 2s complement computers, which is
    why you see it in the wild). The correct way is static_cast<const
    unsigned char&>(c) because you want to cast the bits of the character
    and not the mathematical value of it.
    Me, Jun 8, 2005
    #2
    1. Advertising

  3. bonham

    bonham Guest

    Thanks! this answers more than i asked for.
    I assume the "&" is there to ensure the cast of bits of character than
    the mathematics value in static_cast<const unsigned char&>(c) rite?
    why need the "const"?
    bonham, Jun 8, 2005
    #3
  4. bonham

    bonham Guest

    Thanks! this answers more than i asked for.
    I assume the "&" is there to ensure the cast of bits of character than
    the mathematics value in static_cast<const unsigned char&>(c) rite?
    why need the "const"?
    bonham, Jun 8, 2005
    #4
  5. bonham

    Me Guest

    > Thanks! this answers more than i asked for.
    > I assume the "&" is there to ensure the cast of bits of character than
    > the mathematics value in static_cast<const unsigned char&>(c) rite?
    > why need the "const"?


    static_cast<const unsigned char&>(c) is the same thing (with added
    semantics) as *static_cast<const unsigned char*>(&c) so you can see
    you're basically intepreting memory a different way. The const is there
    for two reasons 1. const safety
    (http://www.parashift.com/c -faq-lite/const-correctness.html) and 2.
    to allow for binding an r-value to a reference
    (http://cpptips.hyperformix.com/cpptips/nconst_tmp_ref3)
    Me, Jun 8, 2005
    #5
  6. bonham

    Me Guest

    > char c;
    > static_cast<const unsigned char&>(c)


    Doh, strike that, that code shouldn't compile (I'm way too used to
    VC6's broken compiler). The correct way is to get the unsigned bits is:

    *static_cast<const unsigned char*>(static_cast<const void*>(&c)) or
    *(const unsigned char*)(const void*)(&c).

    Some people just do reinterpret_cast<unsigned char&>(c) or even just
    (unsigned char&)(c). The C++ standard doesn't guarantee it works (the C
    one does however). Personally, I always cast through void* just to play
    it safe.
    Me, Jun 8, 2005
    #6
  7. bonham

    bonham Guest

    Thank you very much!
    bonham, Jun 8, 2005
    #7
  8. bonham

    sparrow

    Joined:
    Dec 1, 2012
    Messages:
    1
    what about the

    static_cast<unsigned int>
    sparrow, Dec 1, 2012
    #8
    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. GuineaPig
    Replies:
    9
    Views:
    555
    Fraser Ross
    Nov 15, 2003
  2. Siemel Naran

    Re: static_cast signed to unsigned

    Siemel Naran, Apr 1, 2004, in forum: C++
    Replies:
    1
    Views:
    654
    Kevin Goodsell
    Apr 1, 2004
  3. Bo Peng
    Replies:
    11
    Views:
    1,085
    Victor Bazarov
    Oct 20, 2006
  4. pozz
    Replies:
    12
    Views:
    738
    Tim Rentsch
    Mar 20, 2011
  5. junyangzou
    Replies:
    13
    Views:
    257
Loading...

Share This Page