Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Signed char conversion
Forum Updated to NodeBB v4.3 + New Features

Signed char conversion

Scheduled Pinned Locked Moved C++ Gurus
4 Posts 4 Posters 2.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    GrahamL
    wrote on last edited by
    #1

    Hi
    This code gives 'c' a value of 8
    @
    signed char c = 127;
    c = (c + 4) / 16;
    @

    But this code gives -127
    @
    signed char c = 127;
    c = (c + 4);
    c = c / 16;
    @

    This code gives also 8
    @
    signed char c = 127;
    c = (c + 4) / (signed char)16;
    @

    This code gives also 8
    @
    signed char c = 127;
    signed char x = 4;
    signed char y = 16;

    c = (c + x) / y;
    @

    Could someone please explain why this is so

    Thanks

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mcosta
      wrote on last edited by
      #2

      Hi,

      each compiler optimizes written code in order to reduce single CPU operations.
      Probably if you do a single calculation (example 1, 3 e 4) compiler makes optimization and avoid overflow problem identified in case 2.

      However the correct result should be -8 because your code generates an overflow.

      Regards

      Once your problem is solved don't forget to:

      • Mark the thread as SOLVED using the Topic Tool menu
      • Vote up the answer(s) that helped you to solve the issue

      You can embed images using (http://imgur.com/) or (http://postimage.org/)

      1 Reply Last reply
      0
      • G Offline
        G Offline
        giesbert
        wrote on last edited by
        #3

        The correct result would not be -8 for example 2,
        c = 127; // 127
        c += 4; // 131 --> overflow = -125
        c /= 16; // -7

        How the compiler caches temporaries in between, you never know.
        it might be in example 1, that the temp value is stored as int as this is the prefered size on a 32 bit processor.

        In your example 2, you do not work with temporaries but store the result always in the signed int. so You get the overflow problem.

        This was the test code in the debugger:

        @
        signed char c = 127;
        c += 4;
        c /= 16;
        @

        Nokia Certified Qt Specialist.
        Programming Is Like Sex: One mistake and you have to support it for the rest of your life. (Michael Sinz)

        1 Reply Last reply
        0
        • Chris KawaC Offline
          Chris KawaC Offline
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You need to remember that literals like 4 or 16 are signed integers. This is given by the standard.
          @
          4 // int
          4u // unsigned int
          4l // long
          4ul // unsigned long
          ...
          @
          So:
          @
          (c + 4) // c is signed char, 4 is signed integer so a widening conversion is done
          ((int)c + 4) // this is equivalent

          c = (c + 4) // c is signed char, (c + 4) is signed integer, narrowing result to fit
          c = (signed char)(c + 4) //this is equivalent

          (c + 4) / 16 // (c + 4) is signed integer, 16 is signed integer, no conversion
          c = (c + 4) / 16 // (c + 4) / 16 is signed integer, c is signed char, narrowing
          c = (signed char)((c + 4) / 16) // this is equivalent
          @
          ...and so on.

          Reasoning the code like this you will see that your narrowing conversions occur in different places, rendering different results.
          As a side note:
          @//those are equivalent
          (c + 4) / (signed char)16
          ((int)c + 4) / (int)(signed char)16
          @
          so the explicit cast to signed char is pointless as it will be cast back again.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved