2

Bitwise operators in C# get automatically cast to int. For instance :

byte a = 0x0f;
byte b = 0xf0;
byte c = a & b;

Won't compile and renders the following error. Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)

To make it happy you have to cast the result of a & b

byte c = (byte)(a & b);

While it is highly annoying and rather redundant to first automatically cast the bytes to ints then need to manually cast them back it is required because someone decided to leave bitwise operators on byte out of the C# specifications.

Do these extra casts have an effect on efficiency or are they compiled down to nothing?

Would simply using more memory and leaving the result as an int provide any performance benefit?

int c = a & b;
user2163234
  • 527
  • 5
  • 9
  • I do not believe this question has been asked before, and the question linked to is quite a bit different from what I am asking. – user2163234 Dec 22 '15 at 21:26

0 Answers0