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. [solved] Subracting two shorts [modified]

[solved] Subracting two shorts [modified]

Scheduled Pinned Locked Moved C#
helpquestion
7 Posts 4 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.
  • D Offline
    D Offline
    Dwayner79
    wrote on last edited by
    #1

    I have this bit of code:

    short s1 = 10;
    short s2 = 5;
    short s3 = s1 - s2;

    Error says: Error Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Aren't shorts just 16 bit integers? You can't subtract two of them?

    ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

    modified on Sunday, August 30, 2009 9:45 PM

    O P 2 Replies Last reply
    0
    • D Dwayner79

      I have this bit of code:

      short s1 = 10;
      short s2 = 5;
      short s3 = s1 - s2;

      Error says: Error Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Aren't shorts just 16 bit integers? You can't subtract two of them?

      ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

      modified on Sunday, August 30, 2009 9:45 PM

      O Offline
      O Offline
      Obsidian713
      wrote on last edited by
      #2

      int was assumed on the right side of the assignment. MSDN

      1 Reply Last reply
      0
      • D Dwayner79

        I have this bit of code:

        short s1 = 10;
        short s2 = 5;
        short s3 = s1 - s2;

        Error says: Error Cannot implicitly convert type 'int' to 'short'. An explicit conversion exists (are you missing a cast?) Aren't shorts just 16 bit integers? You can't subtract two of them?

        ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

        modified on Sunday, August 30, 2009 9:45 PM

        P Offline
        P Offline
        PIEBALDconsult
        wrote on last edited by
        #3

        For some reason the designers of C# decided that 8- and 16-bit operations should return a 32-bit result rather than the expected data type :mad: .

        D 1 Reply Last reply
        0
        • P PIEBALDconsult

          For some reason the designers of C# decided that 8- and 16-bit operations should return a 32-bit result rather than the expected data type :mad: .

          D Offline
          D Offline
          Dwayner79
          wrote on last edited by
          #4

          Thanks to both. That makes very little sense to me. If anyone knows a good *why* answer, I would love to hear it, but I will just use a cast. Thanks,

          ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

          L 1 Reply Last reply
          0
          • D Dwayner79

            Thanks to both. That makes very little sense to me. If anyone knows a good *why* answer, I would love to hear it, but I will just use a cast. Thanks,

            ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

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

            All integer expressions are computed using ints or larger, even if all the terms in it are bytes or shorts; that has been true in all C-like languages. The difference with C# is it complains when trying to store such expression result in anything smaller than int; C and C++ silently truncated the result, with a possible value error for free. In C# you need a cast, so you know you are responsible for a possible truncation. try this (or its equivalent) in different languages:

            		ushort a=1;
            		ushort b=0xFFFF;
            		a++;
            		a=(ushort)(a+1);
            		b++;
            		b=(ushort)(b+1);
            

            You may not like it, but this is how it has been defined. The details can be found in section 7.2.6.2 of the C# language specification. If you were to study the programming language you choose from a book, you would probably know such things. :)

            Luc Pattyn

            :badger: :jig: :badger:

            Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

            :jig: :badger: :jig:

            D 1 Reply Last reply
            0
            • L Luc Pattyn

              All integer expressions are computed using ints or larger, even if all the terms in it are bytes or shorts; that has been true in all C-like languages. The difference with C# is it complains when trying to store such expression result in anything smaller than int; C and C++ silently truncated the result, with a possible value error for free. In C# you need a cast, so you know you are responsible for a possible truncation. try this (or its equivalent) in different languages:

              		ushort a=1;
              		ushort b=0xFFFF;
              		a++;
              		a=(ushort)(a+1);
              		b++;
              		b=(ushort)(b+1);
              

              You may not like it, but this is how it has been defined. The details can be found in section 7.2.6.2 of the C# language specification. If you were to study the programming language you choose from a book, you would probably know such things. :)

              Luc Pattyn

              :badger: :jig: :badger:

              Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

              :jig: :badger: :jig:

              D Offline
              D Offline
              Dwayner79
              wrote on last edited by
              #6

              Luc, Thanks for your explanation. Not sure if the last line is meant condescendingly, but I value your input too much to complain ;-) . And your right. Its been a while since I really dug through my c# books. This is a PT thing for me. When it was FT, I did spend a lot more time in the books. Thanks again.

              ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

              L 1 Reply Last reply
              0
              • D Dwayner79

                Luc, Thanks for your explanation. Not sure if the last line is meant condescendingly, but I value your input too much to complain ;-) . And your right. Its been a while since I really dug through my c# books. This is a PT thing for me. When it was FT, I did spend a lot more time in the books. Thanks again.

                ***************** "We need to apply 21st-century information technology to the health care field. We need to have our medical records put on the I.T." —GW

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

                Dwayner79 wrote:

                Not sure if the last line is meant condescendingly

                Not at all. Too many people try and learn a language by experimenting, or reading a few things on the web, instead of really studying a book. A book tends to teach such stuff in a logic order, explaining how and why things are the way they are, and IMO there isn't really any alternative. And that's what I wanted to suggest. :)

                Luc Pattyn

                :badger: :jig: :badger:

                Have a look at my entry for the lean-and-mean competition; please provide comments, feedback, discussion, and don’t forget to vote for it! Thank you.

                :jig: :badger: :jig:

                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