[.net] Why isnt the result of bit-wise or ( operator) between two bytes also a byte

Started by
6 comments, last by benryves 13 years, 11 months ago
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 ???
Advertisement
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:

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.

Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes)
DeviantArt :: Because right-brain needs love too (also pretty neglected these days)

Quoting MSDN:

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.
"When you die, if you get a choice between going to regular heaven or pie heaven, choose pie heaven. It might be a trick, but if it's not, mmmmmmm, boy."
How to Ask Questions the Smart Way.
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.
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.
I believe this is the same in C, which can cause to problems on 8-bit architectures.

[Website] [+++ Divide By Cucumber Error. Please Reinstall Universe And Reboot +++]

This topic is closed to new replies.

Advertisement