Remove the implicit conversions associated with bitwise operators #9417
Replies: 4 comments 8 replies
-
There is nothing to "remove". Instead, there's no operator defined on small integers. The implicit conversion happens before the operator. To remove the conversion, there needs to be new operators introduced for byte.
Modern CPU only provides operators on int32. The casts will remain the same after lowering. |
Beta Was this translation helpful? Give feedback.
-
This would be a breaking change. Consider this code: var result = byte1 | byte2; At the moment, If this change went forward, This change might result in different overload selection, or in under/over-flow happening at different boundaries in later expressions. |
Beta Was this translation helpful? Give feedback.
-
Referring back to the original request:
If this happened, the natural type of var result = byte1 | byte2; would result in it being equivalent to byte result = byte1 | byte2; This is how var works: the inferred type of a var statement is the type of the expression result on the righthand side. This would be, as I said, a breaking change. For var result = byte1 | byte2; to compile as int result = byte1 | byte2; requires the righthand side to have type int - which is what the compiler currently does. |
Beta Was this translation helpful? Give feedback.
-
Again you raise excellent points. I'm of the opinion that the inference made by So long as byte result = byte1 | byte2; So I agree that this is an important point to maintain backward compatibility, but I think it can be maintained while at the same time allow the above code. If There are a host of details of course but this is not in and of itself unusual with languages. The core of my position is that operators like |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
When we use bitwise operators with
sbyte
,byte
,short
,ushort
, orchar
operands, the language forces each operand to be converted toint
.This makes sense for arithmetic operators (where the bit length of results might increase) but really doesn't make any sense for bitwise operators.
This refuses to compile today
and must be expressed as
This verbosity seems unnecessary and I wonder if the language team would consider reviewing this behavior.
If these operators were modified to not promote these types to
int
then the casts could be eliminated. It seems too, that this would be a non-breaking change which is why I am raising it (if it were a breaking change then of course it becomes a much more contentious suggestion).I've been encountering this a lot recently because I'm writing C# code that manages IoT devices and there's a lot of byte registers and stuff that need specific bits setting/resetting and it quickly becomes cluttered with all the casts.
I wonder if there's a cost too in converting (for example)
byte
toint
in these expressions, if there is then that cost could be eliminated.Note too that the compound bitwise assignment operators do not need casts and this seems inconsistent for no good reason, here
register
anddatum
are each of typebyte
and this compiles fine:whereas this does not:
Beta Was this translation helpful? Give feedback.
All reactions