[.net] Why isnt the result of bit-wise or ( operator) between two bytes also a byte
Hey guys
I'm confused as to whats going on here.
Byte a = 1;
Byte b = 2;
a = a | b; // compiler error, Cannot implicitly convert type 'int' to 'byte'. An explicit conversion exists (are you missing a cast?)
adding a type cast fixes it fine: a = (byte)(a | b);
However in the documentation: "There is a predefined implicit conversion from byte to short, ushort, int, uint, long, ulong, float, double, or decimal." So it should be doing the type cast automagically.
BUT the bigger question is why would bitwise OR between two bytes return anything other than a byte ???
The implicit conversion is from byte to the other types, but you can't implicitly convert back. This is from the same docs you're referencing:
Apparently you cannot implicitly convert from larger range storage types to byte. Int (Int32) is 32 bits in size, Byte is only 8.
Quote:
The following assignment statement will produce a compilation error, because the arithmetic expression on the right-hand side of the assignment operator evaluates to int by default.Quote:
byte z = x + y; // Error: conversion from int to byte
Apparently you cannot implicitly convert from larger range storage types to byte. Int (Int32) is 32 bits in size, Byte is only 8.
Quoting MSDN:
That's just "the way it is". Operator | always returns an int, so you need to do an explicit cast for byte.
Quote:Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.
That's just "the way it is". Operator | always returns an int, so you need to do an explicit cast for byte.
Okay thanks, I skipped over that bit as i wasn't interested in addition. So it looks like its a odd design feature of .net maybe to subtly push you towards using int to avoid over/underflow bugs.
But it still doesn't make sense for bitwise or. byte | byte is always going to be another byte. So why would the | operator return a int ???
or to put it another way:
int operator | (byte a, byte b) {...}
vs
byte operator | (byte a, byte b) {...}
Anyone know why its way it is ? It's not a biggy, but it's odd and annoying.
But it still doesn't make sense for bitwise or. byte | byte is always going to be another byte. So why would the | operator return a int ???
or to put it another way:
int operator | (byte a, byte b) {...}
vs
byte operator | (byte a, byte b) {...}
Anyone know why its way it is ? It's not a biggy, but it's odd and annoying.
Quote:Original post by MrMark
Okay thanks, I skipped over that bit as i wasn't interested in addition. So it looks like its a odd design feature of .net maybe to subtly push you towards using int to avoid over/underflow bugs.
It's not meant to push you toward int, any more than exceptions are meant to push you toward enclosing everything in a try block with an empty catch block. (Remember, ints can overflow too.) Rather, the one-way conversion is intended to remind you that casting from char to int is NOT something to be done without first considering the potential information loss that can result.
As for why boolean ops are defined for int and long, but not for byte and short? Well, that's simple: the typ- LOOK BEHIND YOU!
*runs away*
From what I understand it's because MSIL doesn't have instructions for arithmetic operators below 32 bits. As for why MSIL doesn't have instructions for those operations, you'll have to find someone else to answer that question.
Quote:
From what I understand it's because MSIL doesn't have instructions for arithmetic operators below 32 bits. As for why MSIL doesn't have instructions for those operations, you'll have to find someone else to answer that question.
My understanding is that it is simply to streamline the instruction set and VES.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement