7
short BitwiseTest(short value)
{
    short test1 = ((value >> 8) & 0xFF);
    short test2 = unchecked((short)((value << 8) & 0xFF00));
    return (test1 | test2);
}

The above code is supposed to be a (inefficient) example that swaps the endianness of a short (signed 16-bit integer) in C#.

However the above code will not compile because C# is implicitly casting from a short to an int on both of the following lines:

First case:

short test1 = ((value >> 8) & 0xFF);

Second case:

return (test1 | test2);

Why is this cast taking place? Would I achieve the expected result simply by casting back to a short? Like so:

short BitwiseTest2(short value)
{
    short test1 = (short)((value >> 8) & 0xFF);
    short test2 = unchecked((short)((value << 8) & 0xFF00));
    return ((short)(test1 | test2));
}

If not why not?

Note that I do understand why C# casts a short to an integer when performing a left bit-shift, hence the assignment of the test2 variable.

Benjamin Dobell
  • 4,042
  • 1
  • 32
  • 44
  • I haven't voted to close as duplicate, but this is significantly similar to http://stackoverflow.com/questions/941584 – Jon Skeet Jan 19 '11 at 14:39
  • Perhaps I should explain why I do not find the answers to existing questions about addition relevant. With addition there is the possibility of overflow, just as there is with the assignment of test2. I understand those circumstances. However in the two situations I've outlined there is no possibility of overflow. – Benjamin Dobell Jan 19 '11 at 14:50

1 Answers1

0

This is basically answered in another answer (even if the question is quite different) by Eric Lippert himself.

Take a look Integer summing blues, short += short problem

Community
  • 1
  • 1
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
  • That question is not even remotely the same, nor has it been marked answered. – Benjamin Dobell Jan 19 '11 at 14:42
  • 1
    @Benjamin: Just for reference, the fact that the original asker hasn't chosen to accept an answer doesn't mean it hasn't been answered. Lots of people just never seem to get around to it, or understand that they are supposed to. You can usually tell more about the quality of a particular answer by the number of votes has received from the community, rather than by whether or not it is accepted. – Cody Gray - on strike Jan 19 '11 at 14:44
  • 1
    The important part of that answer is that it talks about all short operation ending up as an int. As I said, the question is different, but the answer will be basically the same. That short short equals int. – Øyvind Bråthen Jan 19 '11 at 14:47
  • @Cody Gray: That's a very valid point. I should elaborate that the reasoning given behind the proposed answers in the referenced question are not applicable here. – Benjamin Dobell Jan 19 '11 at 14:48
  • @Benjamin - By the way if "not even remotely the same" == "quite different" then I guess we have an agreement ;) – Øyvind Bråthen Jan 19 '11 at 14:48
  • @Øyvind Knobloch-Bråthen: Sorry for the confusion. I don't just mean the wording used by the author of the referenced question is different. I mean the "Stackoverflow concept of a question is different", i.e. the answers, comments and so forth are not related to this question. Well other than the fact both questions are discussing C# syntax intricacies. – Benjamin Dobell Jan 19 '11 at 14:55