How do add 1 to a byte
Asked Answered
K

4

5

I've tried

 module Program
 {
  Main() : void
  { mutable x : byte = 0B;
    mutable y : byte = 0B;  
    x++;
    //y = x + 1;
    //y = x + 1B;
    //def one:byte=1;//   x = x + one;
  }
 }

No matter which one I try, I get the following error message.

Error 1 expected byte, got int in assigned value: System.Int32 is not a subtype of System.Byte [simple require]

Only way I've found that works is

    y = ( x + 1 ):>byte

Which is bit of faff, just to add one.

Why is this? and Is there a better (read shorter way)?

Kendre answered 10/7, 2011 at 20:58 Comment(2)
I don't use nermerle, but it sounds like a "type widening" issue -- what is the return type of byte + int? What of byte + byte? C# is similar: byte x = 0; x = x + 1 will also not compile. (C# will actually work okay with x += 1 and x++, but it may be the case that these are expanded in nemerle.)Nitre
byte + int = int (int is the dominant type). byte + byte is the unexpected return type of an intKendre
C
8

As in C#, the result of the sum of a byte and a byte in Nemerle is an int. However, unlike C#, Nemerle tries to keep the core language as compact as possible, keeping all of the syntax sugar in the standard macro library. In this spirit, the += and ++ operators are macros that are translated into regular addition.

To answer your question, (x + 1) :> byte is the way to do it. It actually is not all bad, because it lets the reader of your code know that you are aware of the danger of overflow and take responsibility for it.

Still, if you feel strongly about it, you can easily write your own += and ++ macros to perform the cast. It would only take a few lines of code.

Calli answered 10/7, 2011 at 22:3 Comment(0)
H
2

It is because the CLR defines only a limited number of valid operands for the Add IL instruction. Valid are Int32, Int64, Single and Double. Also IntPtr but that tends to be disabled in many languages.

So adding a constant to a byte requires the byte to be converted to an Int32 first. The result of the addition is an Int32. Which doesn't fit back into a byte. Unless you use a bigger hammer. This is otherwise healthy, the odds that you overflow Byte.MaxValue are pretty large.

Note that there are languages that automatically cast, VB.NET is one of them. But it also automatically generates an OverflowException. Clearly not the one you are using, nor C#. This is a perf choice, the overflow test is not that cheap.

Hassan answered 10/7, 2011 at 21:33 Comment(4)
y = if(x<255){x + 1}else{255}; is not too hard to write. So take it C# is implicitly widening casting a byte to int (secretly), which to be is bit against the spirit type safety. You'd think you could do simple additions with the "basic" types without the casting.Kendre
All languages widen the byte. They have to, there is no add operator that takes a byte. This was key to my answer. Your workaround is questionable, I would throw OverflowException but will put up with 0. 255 makes no sense.Hassan
Not if you want bounded values.Kendre
That's just a pretty name for "bug". No way that the CLR designers could ever commit to that behavior. An Add that doesn't add anything is a litany of bug reports. You are free to inject the behavior yourself.Hassan
M
1

Disclaimer: I don't know Nemerle, but I'm assuming it behaves similar to C# in this regard.

There is a better and shorter way: don't use bytes.

Why are you using them in the first place? Most of the time, computations with ints are faster than using bytes, because today's computers are optimized for them.

Monobasic answered 10/7, 2011 at 21:6 Comment(4)
can you explain why int based calculations are faster then byte based ones?Asmodeus
@Monobasic Doh! I would never have thought of that. (sarcasm). I'm using bytes. You can add one to byte in C# and it works, why not Nemerle?Kendre
@Adam, you can't do that in C#.Monobasic
found the developer with no contact to hardware. working with bytes and stuff becomes important when you have an interface to hardware which expects stuff like that.Quondam
B
0

This is what the type system does. Additions upgrade values to ints and then performs the operation resulting in a int. Also have thought what should happen if starting value was 255?

Bacteroid answered 10/7, 2011 at 21:9 Comment(3)
I'd take that into account, I stripped down the code to minimum required to replicate the symptoms.Kendre
So why it x++ valid? Why does it cast it back down to an byte?Kendre
VB.Net valid byte addition. Option Strict On Option Explicit On Module Module1 Sub Main() Dim x As Byte = 1 Dim y As Byte = 0 Dim o As Byte = 1 y = x + o End Sub End Module .Kendre

© 2022 - 2024 — McMap. All rights reserved.