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#
  4. what is the value if u add a value more than the maximum value for the data type

what is the value if u add a value more than the maximum value for the data type

Scheduled Pinned Locked Moved C#
question
5 Posts 3 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.
  • C Offline
    C Offline
    cyn8
    wrote on last edited by
    #1

    hi, may i know what suppose to be the answer for the below code and its reason for it? Int16 minValue=-32768; Int16 maxValue=32767; Int16 delta=2; Int16 Value = (Int16)(minValue - delta) Int16 Value = (Int16)(maxValue + delta) Thanks in advance.

    C L 2 Replies Last reply
    0
    • C cyn8

      hi, may i know what suppose to be the answer for the below code and its reason for it? Int16 minValue=-32768; Int16 maxValue=32767; Int16 delta=2; Int16 Value = (Int16)(minValue - delta) Int16 Value = (Int16)(maxValue + delta) Thanks in advance.

      C Offline
      C Offline
      CKnig
      wrote on last edited by
      #2

      this looks like your homework - why don't you give a try?

      C 1 Reply Last reply
      0
      • C CKnig

        this looks like your homework - why don't you give a try?

        C Offline
        C Offline
        cyn8
        wrote on last edited by
        #3

        it is not my homework, just something that i encounter while programming...i want to know how the adding and minus works....how can it be negative number when the delta is 1 and it is added to the max value?

        C 1 Reply Last reply
        0
        • C cyn8

          it is not my homework, just something that i encounter while programming...i want to know how the adding and minus works....how can it be negative number when the delta is 1 and it is added to the max value?

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

          ok, this is because the numbers are just a bunch of bits in memory (16 in your case) and the negative numbers are those starting (or ending ;) with 1 - for example: (100101....) - so if you add a value and the result is bigger the max pos value the last 1 will be set and alas the value will be negative.

          1 Reply Last reply
          0
          • C cyn8

            hi, may i know what suppose to be the answer for the below code and its reason for it? Int16 minValue=-32768; Int16 maxValue=32767; Int16 delta=2; Int16 Value = (Int16)(minValue - delta) Int16 Value = (Int16)(maxValue + delta) Thanks in advance.

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, for most (if not all) data types MaxValue+1 and MinValue have the same bit representation. If your code is not detecting overflows, incrementing MaxValue will just result in Minvalue; if your code is organized to detect overflow, an OverflowException will be thrown. :)

            Luc Pattyn [Forum Guidelines] [My Articles]


            Sorry for any delays in replying, I currently don't always get e-mail notifications.


            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