7

I got shocked knowing that there are no arithmetic operator +, -, *, /, and % for 8 and 16 bit integers in C#. I am reading "C# 5.0 Pocket Reference" on page 23 as follows.

enter image description here

The following code does not compile.

class Program
{
    static void With16Bit()
    {
        short a = 1;
        short b = 2;
        short c = a + b;
        Console.WriteLine(c);
    }

    static void With8Bit()
    {
        byte a = 1;
        byte b = 2;
        byte c = a + b;
        Console.WriteLine(c);
    }

    static void Main(string[] args)
    {
        With8Bit();
        With16Bit();
    }
}

Why did the C# designers do it? What are their consideration about it?

kiss my armpit
  • 3,413
  • 1
  • 27
  • 50
  • 4
    Answered in this comment by Eric Lippert http://stackoverflow.com/questions/941584/byte-byte-int-why/941627#comment750078_941584 – keyboardP Feb 27 '14 at 13:04

3 Answers3

10

There are arithmetics with Int8, Int16; but the result is int32 and so you have to cast:

class Program
{
    static void With16Bit()
    {
        short a = 1;
        short b = 2;
        short c = (short) (a + b); // <- cast, since short + short = int
        Console.WriteLine(c);
    }

    static void With8Bit()
    {
        byte a = 1;
        byte b = 2;
        byte c = (byte) (a + b); // <- cast, since byte + byte = int
        Console.WriteLine(c);
    }

    static void Main(string[] args)
    {
        With8Bit();
        With16Bit();
    }
}
Svish
  • 152,914
  • 173
  • 462
  • 620
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • 3
    Yes, but that was not his question: "Why did the C# designers do it? What are their consideration about it?" – Dirk Feb 27 '14 at 13:06
  • This explains why is doesn't compile but doesn't answer why the designers did not overload the operators for bytes, sbyte, shorts, ushorts. – keyboardP Feb 27 '14 at 13:06
  • @keyboardP There **are** overloads, it's just a cast error. – ken2k Feb 27 '14 at 13:07
  • @Dirk: I interpreted the question as "Why can't I add up bytes/shorts" which this question answers by saying that you can. The OP gives no indication that they are aware that it is just a casting problem causing their issue. – Chris Feb 27 '14 at 13:11
  • @ken2k - Right you are. I was thinking of the `+` operator in the example which I don't think exists for bytes. – keyboardP Feb 27 '14 at 13:13
5

Plese remember that when you perform addition operation on short and byte the default outcome would be Integer.

So the :

byte + byte = int
short + short = int

So if you want to get back the actual value you need to cast it back.

Try This:

       short a = 1;
       short b = 2;
       short c =(short) (a + b);
       Console.WriteLine(c);

       byte a = 1;
       byte b = 2;
       byte c =(byte) (a + b);
       Console.WriteLine(c);

From The Source:

This Behaviour is because the designers have not considered that byte and short as actual numbers ,but they have considered them as only sequence of bits. so performing arithmetic operations on them does not make any sense so if that is the case int and long would serve the purpose.

Community
  • 1
  • 1
Sudhakar Tillapudi
  • 25,935
  • 5
  • 37
  • 67
2

From kek444 answer

All operations with integral numbers smaller than Int32 are widened to 32 bits 
before calculation by default. The reason why the result is Int32 is
simply to leave it as it is after calculation. If you check the
MSIL arithmetic opcodes, the only integral numeric type they operate 
with are Int32 and Int64. It's "by design".
Community
  • 1
  • 1
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53