Typecasting of a unsigned value to signed integer

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jeyshree
    New Member
    • Jul 2010
    • 75

    Typecasting of a unsigned value to signed integer

    Hello,

    For the snippet below
    Code:
    unsigned int tmp1=65535;
    unsigned int tmp2=0,tmp3=0;
    tmp2=(char)tmp1;
    tmp3=(int)tmp1;
    printf("tmp1=%u\ntmp2=%u\ntmp3=%u\n",tmp1,tmp2,tmp3);
    The output of the code was
    tmp1=65535
    tmp2=255
    tmp3=65535


    The binary value of 65535 is 1111 1111 1111 1111.


    So when we do tmp2=(uint8)tmp 1, the upper 8 bits are not considered as it is typecasted to uint8.
    So I understand tmp2 is assigned 255.

    But when we do tmp3=(sint8)tmp 1 ,I dont know how the output of 65535 is produced?
    (sint16)1111 1111 1111 1111===>0000 0001(2's complement of 1111 1111).

    when this is assigned to tmp3 the output should have been 255.I dont understand how 65535 comes.

    I guess the calculation goes as follows:
    1111 1111 =============== =============== >0000 0001
    2's complement of lower byte

    0000 0001=========== ======>0000 0000 0000 0001
    (32 bit notation)

    0000 0000 0000 0001=========== >1111 1111 1111 1111
    2's comp


    [But I dont understand why the last 2's complement calculation is carried out.The above 3 calculations are just my ideas.If this idea is wrong please let me know where I am going wrong]
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I'm just about ready to write a C++ Insight on the reasons why you never cast. This is one example.

    First, casting changes nothing. The compiler flags an error because types don't match and rather than code around the error, the programmer does a typecast which tells the compiler to forget about the rules and stop putting out this error. It's based on the supposition that the programmer knows more about what's going on than the compiler does.

    In this case, an unsigned integer cannot be cast to a signed integer since the left bit is data whereas in a signed integer the left bit is the sign bit. Further, if the sign bit is ON then the data is usually in 2's complement format since it is a negative number.

    Therefore, the bits change their value between the two formats.

    Since there's no way to control the value in the unsigned int without doing extra code to insure the left bit is always OFF, the typecast is very ill-advised.

    Can you not just use a signed int?

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      I don't recognize uint, uint8, or sint8. I'll assume that these are equivalent to unsigned int, unsigned char, and signed char respectively.

      Line 3 converts a 16-bit unsigned value into an 8-bit unsigned value and then assigns that result to an unsigned int. The C Standard requires that the result of the first conversion be the original value mod 2^8; so this result can be counted on to be 255. 255 can be represented by an unsigned int so the assignment doesn't change anything,

      Line 4 converts a 16-bit unsigned value into an 8-bit signed value and then assigns that result to an unsigned int. The first of these conversions is undefined because the original value cannot be represented with an 8-bit signed value, so don't waste your time trying to make sense out of what happens. (The C Standard has different rules for when the destination type is signed or unsigned.)

      Comment

      • jeyshree
        New Member
        • Jul 2010
        • 75

        #4
        hello,
        Sorry i gave the wrong syntax.I understand your point.It was a snippet to just get some basic idea abou type casting.

        Comment

        • jeyshree
          New Member
          • Jul 2010
          • 75

          #5
          Hello,

          I corrected the syntax now.Thanks for your reply

          Comment

          Working...