How come short + int = int but short + uint = long?

This is a discussion on How come short + int = int but short + uint = long? within the C# Programming forums, part of the General Programming Boards category; Code: static void Main(string[] args) { short a = 10; int x = 10; uint y = 10; Console.WriteLine((a + ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    71

    How come short + int = int but short + uint = long?

    Code:
       static void Main(string[] args)
            {
                short a = 10;
                int x = 10;
                uint y = 10;
    
                Console.WriteLine((a + x).GetType());//System.Int32
                Console.WriteLine((a + y).GetType());//System.Int64 ?
                Console.ReadKey();
            }
    Can someone explain?

    while we are at it, check this one out:

    Code:
            static void Main(string[] args)
            {
                short a = 10;
                ushort b = 10;
    
                int x = 10;
                uint y = 10u;
    
                long j = 10;
                ulong k = 10;
    
                Console.WriteLine((a * b).GetType());//System.Int32
                Console.WriteLine((x * y).GetType());//System.Int64
                Console.WriteLine((j * k).GetType());//won't compile
                Console.ReadKey();
            }
    I know that arithmetic operations on integers (signed & unsigned) and floating points will result in an expression of floating point type.

    I also know that arithmetic operations on single/double and decimals is not permitted.

    I also know that arithmetic operations on integers of different bits will result in an expression of type equal to that of the integer with the higher number of bits.

    But arithmetic operations involving unsigned integers result in expressions of unexpected type. Can someone explain why that's the case?
    Last edited by y99q; 10-28-2011 at 09:23 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You probably want this: 7.2.6.2 Binary numeric promotions (C#)


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    The logic behind this is simple:

    1. If one operand is signed, all operands must be converted to signed.

    2. An unsigned operand has to be promoted to the next larger signed type, because otherwise half of its possible values would overflow.

    For example, an unsigned 8-bit number can have values from 0 to 255. A signed 8-bit number can only represent numbers as high as 127, because half of the signed type's range is below zero. So if an unsigned 8-bit number is converted to a signed, it needs at least 16 bits to guarantee the number can be represented.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Short/long/signed/unsigned int
    By scuzzo84 in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 12:40 AM
  2. long question; short answer <templates>
    By misplaced in forum C++ Programming
    Replies: 4
    Last Post: 09-25-2004, 06:34 AM
  3. Replies: 8
    Last Post: 05-13-2003, 03:47 PM
  4. short and long int
    By gogo in forum C Programming
    Replies: 3
    Last Post: 03-10-2003, 06:25 AM
  5. bytes long and short
    By SAMSAM in forum Game Programming
    Replies: 0
    Last Post: 03-08-2003, 09:50 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21