Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do add 1 to a byte

Tags:

.net

nemerle

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)?

like image 386
Adam Speight Avatar asked Jul 10 '11 20:07

Adam Speight


People also ask

How do you add 1 byte?

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 .

What is byte addition?

The addition of two-byte values in java is the same as normal integer addition. The byte data type is 8-bit signed two's complement integer. It has a minimum value of -128 and a maximum value of 127 (inclusive).

Which of the following could represent 1 byte?

A byte is not just 8 values between 0 and 1, but 256 (28) different combinations (rather permutations) ranging from 00000000 via e.g. 01010101 to 11111111 . Thus, one byte can represent a decimal number between 0(00) and 255.

What is a byte [] in Java?

Definition and Usage. The byte keyword is a data type that can store whole numbers from -128 to 127.


2 Answers

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.

like image 61
Don Reba Avatar answered Sep 30 '22 10:09

Don Reba


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.

like image 23
Hans Passant Avatar answered Sep 30 '22 09:09

Hans Passant