Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. uint32_t result of uint16_t addition

uint32_t result of uint16_t addition

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 5 Posters 0 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.
  • E Offline
    E Offline
    elelont2
    wrote on last edited by
    #1

    Hi, im having trouble understanding this line of C code (XC32, pic32):

    uint16_t Cur = 65535;
    uint16_t Next = 14000;
    uint32_t ResVal = 0;
    ResVal = ((uint32_t)Cur + (uint32_t)Next);

    I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?

    J J C R 4 Replies Last reply
    0
    • E elelont2

      Hi, im having trouble understanding this line of C code (XC32, pic32):

      uint16_t Cur = 65535;
      uint16_t Next = 14000;
      uint32_t ResVal = 0;
      ResVal = ((uint32_t)Cur + (uint32_t)Next);

      I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      That should work even without casting. For your example an optimising compiler would just assign 79535 to ResVal. The XC32 compiler is gcc based. So I don't think that there is a compiler problem.

      1 Reply Last reply
      0
      • E elelont2

        Hi, im having trouble understanding this line of C code (XC32, pic32):

        uint16_t Cur = 65535;
        uint16_t Next = 14000;
        uint32_t ResVal = 0;
        ResVal = ((uint32_t)Cur + (uint32_t)Next);

        I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?

        J Offline
        J Offline
        jeron1
        wrote on last edited by
        #3

        In addition to what Jochen said, sometimes looking at the disassembly is helpful.

        "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

        1 Reply Last reply
        0
        • E elelont2

          Hi, im having trouble understanding this line of C code (XC32, pic32):

          uint16_t Cur = 65535;
          uint16_t Next = 14000;
          uint32_t ResVal = 0;
          ResVal = ((uint32_t)Cur + (uint32_t)Next);

          I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          I tried your code with the simulator (I don't have a an actual PIC32 MCU) and I see (with a watch) the correct result.

          1 Reply Last reply
          0
          • E elelont2

            Hi, im having trouble understanding this line of C code (XC32, pic32):

            uint16_t Cur = 65535;
            uint16_t Next = 14000;
            uint32_t ResVal = 0;
            ResVal = ((uint32_t)Cur + (uint32_t)Next);

            I believe the C standard requires that some sort of integral promotion takes place here but the result is not 79535. Instead i get an overflown uint16_t result. How can i add two uint16_t variables and get the correct uint32_t result?

            R Offline
            R Offline
            R Erasmus
            wrote on last edited by
            #5

            First of all: Casting happens implicitly so you don't need to cast in this scenario. Secondly: These types that you are specifying in non-standard types, so uint32_t could be defined as anything which will give you an incorrect result. Thirdly: How to you check what the result is? Though debugging or though a print statement? The print statement could be wrong. Fouth: Know your platform you writing the code for (its limitations and the standard it is using). Not necessarily that the pic32 uses the specific c standard which you are looking at.

            "Program testing can be used to show the presence of bugs, but never to show their absence." << please vote!! >>

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups