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. Unsigned/Signed

Unsigned/Signed

Scheduled Pinned Locked Moved C / C++ / MFC
c++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.
  • F Offline
    F Offline
    Fareed Rizkalla
    wrote on last edited by
    #1

    So a value declared int in C++. Is signed. However if I choose unsigned and assigned a negative value. It should be zero. Instead I get a large value, based on the most significant bit being 1 (representing the sign value). And whatever was the original value being assigned is deducted from that value. So -0, if nothing is to be deducted. Someone should it be doing this?

    T C 2 Replies Last reply
    0
    • F Fareed Rizkalla

      So a value declared int in C++. Is signed. However if I choose unsigned and assigned a negative value. It should be zero. Instead I get a large value, based on the most significant bit being 1 (representing the sign value). And whatever was the original value being assigned is deducted from that value. So -0, if nothing is to be deducted. Someone should it be doing this?

      T Offline
      T Offline
      Tim Craig
      wrote on last edited by
      #2

      C++ does no checking at runtime when doing automatic conversions. The bits from the signed int are simply crammed into the unsigned int. A negative integer has the high bit set so in the unsigned world it looks like a large number. If you set the warning level to 4, a good idea in general, with VC++ you'll get a signed/unsigned mismatch warning at compile time. You should look at all warnings to make sure you're not shooting yourself in the foot accidentally and fix them with proper casts, etc, so your compiles are totally clean. That will save you a lot of work in the long run looking for subtle bugs like the wrap around you're mentioning.

      Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.

      O 1 Reply Last reply
      0
      • T Tim Craig

        C++ does no checking at runtime when doing automatic conversions. The bits from the signed int are simply crammed into the unsigned int. A negative integer has the high bit set so in the unsigned world it looks like a large number. If you set the warning level to 4, a good idea in general, with VC++ you'll get a signed/unsigned mismatch warning at compile time. You should look at all warnings to make sure you're not shooting yourself in the foot accidentally and fix them with proper casts, etc, so your compiles are totally clean. That will save you a lot of work in the long run looking for subtle bugs like the wrap around you're mentioning.

        Once you agree to clans, tribes, governments...you've opted for socialism. The rest is just details.

        O Offline
        O Offline
        oggenok64
        wrote on last edited by
        #3

        Yes, it should most definitely be doing this! What you are assigning to a variable is a bit pattern - not a value. Whether the variable is signed or not is a question of interpretation. Your interpretation! Assigning a negative constant to an unsigned variable is perfectly legitimate. As Tim Craig pointed out, you should trust your beloved and most obedient servant - the compiler. All compilers will warn you about signed/unsigned mismatch. - turin

        J 1 Reply Last reply
        0
        • F Fareed Rizkalla

          So a value declared int in C++. Is signed. However if I choose unsigned and assigned a negative value. It should be zero. Instead I get a large value, based on the most significant bit being 1 (representing the sign value). And whatever was the original value being assigned is deducted from that value. So -0, if nothing is to be deducted. Someone should it be doing this?

          C Offline
          C Offline
          Chris Losinger
          wrote on last edited by
          #4

          Fareed Rizkalla wrote:

          It should be zero.

          this requirement exists only in your mind. C/C++ does not work that way

          image processing toolkits | batch image processing

          1 Reply Last reply
          0
          • O oggenok64

            Yes, it should most definitely be doing this! What you are assigning to a variable is a bit pattern - not a value. Whether the variable is signed or not is a question of interpretation. Your interpretation! Assigning a negative constant to an unsigned variable is perfectly legitimate. As Tim Craig pointed out, you should trust your beloved and most obedient servant - the compiler. All compilers will warn you about signed/unsigned mismatch. - turin

            J Offline
            J Offline
            josda1000
            wrote on last edited by
            #5

            Just FYI, if you want to be sure that the author gets this message, reply to the author's post and you can refer to Craig's post, as you did in the text. The author didn't get the email, Tim Craig did.

            Josh Davis
            This is what plays in my head when I finish projects.

            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