-2

I know this is a weird question but what is more weird is the behaviour of .NET about Byte data type.

This is quite possible in VB.NET:

    For b As Byte = 14 To 88 Step +10
        Console.WriteLine("Byte=" + b.ToString)
    Next

But this is not:

    For b As Byte = 88 To 14 Step -10
        Console.WriteLine("Byte=" + b.ToString)
    Next

I would understand if the first example couldn't be possible.

Also, as far as I lookup up, it is not possible to increase or decrease a byte value like this:

Dim b As Byte = 56

b += 1 'not possible
b -= 1 'not possible

So, is this the only way to add/subtract a byte value?

b = CByte(b + 5)
b = CByte(b - 5)

I am asking this because the performance is DEADLY critical for my project, I'm coding a Chess engine and I must think about the memory usage all the times so it's best to store the board info in bytes as possible.

But I have concerns about using CByte() once in a while...

AbyxDev
  • 1,363
  • 16
  • 30
Roni Tovi
  • 828
  • 10
  • 21
  • Uhm, unless there is a benchmark, don't worry. You'd be just as well using integers. – user2864740 Mar 14 '15 at 04:53
  • Chess engines have benchmark tests. – Roni Tovi Mar 14 '15 at 04:53
  • Does *yours* have any? Don't worry about it. Anyway, don't worry about `CByte(intExpression)` as it's the same as (in C#) `(byte)intExpression`: "These functions are compiled inline, meaning the conversion code is part of the code that evaluates the expression.." – user2864740 Mar 14 '15 at 04:54
  • Yes, course. Dublicating a whole board info by 1 million between 7 to 8 seconds atm, based on byte values using 850 MB of memory. – Roni Tovi Mar 14 '15 at 04:55
  • 4
    If you're asking these sorts of questions you're a long way from even finding the issues that matter.. anyway, "It doesn't matter, use [CByte](https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx) when you need it". Or, just use an [Integer](https://msdn.microsoft.com/en-us/library/06bkb8w2.aspx) and call it a day (it merely avoids the cast which is required because `b + 5` and `b - 5` *widen* the expression to an Integer). – user2864740 Mar 14 '15 at 04:55
  • Dude, I didn't ask about my abilities here. You wouldn't know... And you best stop commenting about the things you are not even sure. – Roni Tovi Mar 14 '15 at 04:58
  • Mmm, yeah. Read the linked docs yet? – user2864740 Mar 14 '15 at 04:59
  • Like this? http://www.tutorialspoint.com/vb.net/vb.net_data_types.htm This is why a chess engine MUST use bytes. Go search about chess programming and you'll see. Doh... – Roni Tovi Mar 14 '15 at 05:02
  • Uhm, why does that mean a chess engine "must" use a byte? Anyway, the performance will be as good (or better) when using an Integer for all intermediate math. Then just use CByte when storing the value back into an *array* (or List which uses an array backing) structure for space savings. – user2864740 Mar 14 '15 at 05:02
  • Because after it starts calculating (using hashtables) it must store the positions in order to go FURTHER and keep calculating above that position. (called "depth") So the memory usage gets huge amounts by the 10th move ahead. – Roni Tovi Mar 14 '15 at 05:05
  • 1
    OK, have fun! I've explained why CByte doesn't matter (when converting from an Integer), what it does, why it is needed, and how it can be used merely at the boundaries to save space *when* it is in densely packed array. (As soon as the byte values are not stored in an array or other *dense* schema the other overhead of object management makes the 3 saved bytes largely *irrelevant*.) – user2864740 Mar 14 '15 at 05:05
  • Well, the only thing that makes sense in your comments was that statement. You could just say CByte() does not create a performance issue, I'd understand. – Roni Tovi Mar 14 '15 at 05:07
  • 3
    How do you expect the value `-10` to be stored in a byte? Why so reluctant to use CByte? Have you done any research on the subject? Looked at the compiled IL? Done some profiling? And how is it possible that sometimes a 32 bit int is preferred over a 16/8 bit int? http://stackoverflow.com/a/129817/1842065 – Bjørn-Roger Kringsjå Mar 14 '15 at 07:22

1 Answers1

1

The important thing to understand here is that the code:

For b As Byte = 14 To 88 Step 10
    Console.WriteLine("Byte=" + b.ToString)
Next

...is effectively the same as:

    Dim b As Byte = 14
Ret:
    Console.WriteLine("Byte=" + b.ToString)
    b = b + 10
    If b <= 88 then
        Goto Ret:
    End If

The important part to concentrate on is the b = b + 10. Since b is a byte it is an "unsigned" type and it is attempting to perform the addition with 10 as a byte. But when you try to step down, i.e. -10, then it is trying to treat -10 as a byte, but that would require a signed byte so, hence, it fails.

If you write your code like this:

For b As SByte = 88 To 14 Step -10
    Console.WriteLine("Byte=" + b.ToString)
Next

...it works fine.

Note the use of SByte ("signed byte").

Enigmativity
  • 113,464
  • 11
  • 89
  • 172