R RB Smissaert Well-Known Member Licensed User Longtime User Jun 8, 2020 #1 Could somebody tell why this fails? Code: Sub TestByteLoop Dim i As Byte Dim arrBytes(128) As Byte For i = 1 To 127 Log(i) arrBytes(i) = i '<< how can i go past 127 here? Log(arrBytes(i)) 'If i = 127 Then Exit 'this fixes it, but why is it needed? Next End Sub RBS
Could somebody tell why this fails? Code: Sub TestByteLoop Dim i As Byte Dim arrBytes(128) As Byte For i = 1 To 127 Log(i) arrBytes(i) = i '<< how can i go past 127 here? Log(arrBytes(i)) 'If i = 127 Then Exit 'this fixes it, but why is it needed? Next End Sub RBS
LucaMs Expert Licensed User Longtime User Jun 8, 2020 #2 RB Smissaert said: Could somebody tell why this fails? Code: Sub TestByteLoop Dim i As Byte Dim arrBytes(128) As Byte For i = 1 To 127 Log(i) arrBytes(i) = i '<< how can i go past 127 here? Log(arrBytes(i)) 'If i = 127 Then Exit 'this fixes it, but why is it needed? Next End Sub RBS Click to expand... I don't understand the problem. If you declare your array that way (128) it will have positions (index) between 0 and 127. Upvote 0
RB Smissaert said: Could somebody tell why this fails? Code: Sub TestByteLoop Dim i As Byte Dim arrBytes(128) As Byte For i = 1 To 127 Log(i) arrBytes(i) = i '<< how can i go past 127 here? Log(arrBytes(i)) 'If i = 127 Then Exit 'this fixes it, but why is it needed? Next End Sub RBS Click to expand... I don't understand the problem. If you declare your array that way (128) it will have positions (index) between 0 and 127.
LucaMs Expert Licensed User Longtime User Jun 8, 2020 #3 LucaMs said: I don't understand the problem. Click to expand... Understood; this is worse: B4X: Sub aaaaa Dim i As Byte For i = 0 To 255 If i > 127 And i < 131 Then Log(i) End If Next End Sub Upvote 0
LucaMs said: I don't understand the problem. Click to expand... Understood; this is worse: B4X: Sub aaaaa Dim i As Byte For i = 0 To 255 If i > 127 And i < 131 Then Log(i) End If Next End Sub
LucaMs Expert Licensed User Longtime User Jun 8, 2020 #4 It seems that despite this documentation: For Next loops only work with Int or higher (long) Upvote 0
Thabo Maqelepo New Member Jun 8, 2020 #5 RB Smissaert said: Could somebody tell why this fails? Code: Sub TestByteLoop Dim i As Byte Dim arrBytes(128) As Byte For i = 1 To 127 Log(i) arrBytes(i) = i '<< how can i go past 127 here? Log(arrBytes(i)) 'If i = 127 Then Exit 'this fixes it, but why is it needed? Next End Sub RBS Click to expand... Your array has a has a static width (128) if you want more, initialize it with a larger value 256, 512 etc etc Upvote 0
RB Smissaert said: Could somebody tell why this fails? Code: Sub TestByteLoop Dim i As Byte Dim arrBytes(128) As Byte For i = 1 To 127 Log(i) arrBytes(i) = i '<< how can i go past 127 here? Log(arrBytes(i)) 'If i = 127 Then Exit 'this fixes it, but why is it needed? Next End Sub RBS Click to expand... Your array has a has a static width (128) if you want more, initialize it with a larger value 256, 512 etc etc
R RB Smissaert Well-Known Member Licensed User Longtime User Jun 8, 2020 #7 LucaMs said: B4XBytesBuilder Click to expand... I am using that and it is very useful, but not sure how that answers the question. I understand that byte is poor choice for a loop iterator, but how does i become -128 if the loop limit is set at 127? RBS Upvote 0
LucaMs said: B4XBytesBuilder Click to expand... I am using that and it is very useful, but not sure how that answers the question. I understand that byte is poor choice for a loop iterator, but how does i become -128 if the loop limit is set at 127? RBS
agraham Expert Licensed User Longtime User Jun 8, 2020 #8 Bytes are signed values in Java/B4X so have a maximum positive value of 127 and a minimum negative value of -128. The loop exits when the loop counter is incremented past the limit. If you increment a byte value of 127 it overflows to -128. Upvote 0
Bytes are signed values in Java/B4X so have a maximum positive value of 127 and a minimum negative value of -128. The loop exits when the loop counter is incremented past the limit. If you increment a byte value of 127 it overflows to -128.
R RB Smissaert Well-Known Member Licensed User Longtime User Jun 8, 2020 #9 agraham said: Bytes are signed values in Java/B4X so have a maximum positive value of 127 and a minimum negative value of -128. The loop exits when the loop counter is incremented past the limit. If you increment a byte value of 127 it overflows to -128. Click to expand... Yes, I understand that, but why does it enter one more iteration if the iterator has already reached 127? You can see this if you step through the code in debug mode. RBS Upvote 0
agraham said: Bytes are signed values in Java/B4X so have a maximum positive value of 127 and a minimum negative value of -128. The loop exits when the loop counter is incremented past the limit. If you increment a byte value of 127 it overflows to -128. Click to expand... Yes, I understand that, but why does it enter one more iteration if the iterator has already reached 127? You can see this if you step through the code in debug mode. RBS
agraham Expert Licensed User Longtime User Jun 8, 2020 #10 agraham said: The loop exits when the loop counter is incremented past the limit. Click to expand... The test for exiting the loop is greater (or less) than, not equality. The counter is incremented (or decremented) at the end of the loop and the test then made whether to continue the loop or not. Upvote 0
agraham said: The loop exits when the loop counter is incremented past the limit. Click to expand... The test for exiting the loop is greater (or less) than, not equality. The counter is incremented (or decremented) at the end of the loop and the test then made whether to continue the loop or not.
R RB Smissaert Well-Known Member Licensed User Longtime User Jun 8, 2020 #11 agraham said: The test for exiting the loop is greater (or less) than, not equality. The counter is incremented (or decremented) at the end of the loop and the test then made whether to continue the loop or not. Click to expand... Still don't get this. If the counter is -128 it is outside the specified range and it still continues. RBS Upvote 0
agraham said: The test for exiting the loop is greater (or less) than, not equality. The counter is incremented (or decremented) at the end of the loop and the test then made whether to continue the loop or not. Click to expand... Still don't get this. If the counter is -128 it is outside the specified range and it still continues. RBS
agraham Expert Licensed User Longtime User Jun 8, 2020 #12 In an incrementing loop the test is greater than (in a decrementing loop it is less than). -128 is not greater than 127 so the loop continues. Upvote 0
In an incrementing loop the test is greater than (in a decrementing loop it is less than). -128 is not greater than 127 so the loop continues.
R RB Smissaert Well-Known Member Licensed User Longtime User Jun 8, 2020 #13 agraham said: In an incrementing loop the test is greater than (in a decrementing loop it is less than). -128 is not greater than 127 so the loop continues. Click to expand... OK, thanks, I thought it would consider the range. All cleared up then. RBS Upvote 0
agraham said: In an incrementing loop the test is greater than (in a decrementing loop it is less than). -128 is not greater than 127 so the loop continues. Click to expand... OK, thanks, I thought it would consider the range. All cleared up then. RBS
Sagenut Expert Licensed User Longtime User Jun 8, 2020 #14 Can the problem be that i is declared as Byte and then used as the For variable? Maybe just use a different variable in For Cycle. Upvote 0
Can the problem be that i is declared as Byte and then used as the For variable? Maybe just use a different variable in For Cycle.
techknight Well-Known Member Licensed User Longtime User Jun 8, 2020 #15 Ya all bytes in Java are signed. Unlike C where you can declare a uint8 Also your index needs to start at 0, not 1. so 0 to 127. Upvote 0
Ya all bytes in Java are signed. Unlike C where you can declare a uint8 Also your index needs to start at 0, not 1. so 0 to 127.
R RB Smissaert Well-Known Member Licensed User Longtime User Jun 8, 2020 #16 techknight said: Ya all bytes in Java are signed. Unlike C where you can declare a uint8 Also your index needs to start at 0, not 1. so 0 to 127. Click to expand... > Also your index needs to start at 0, not 1 Why? RBS Upvote 0
techknight said: Ya all bytes in Java are signed. Unlike C where you can declare a uint8 Also your index needs to start at 0, not 1. so 0 to 127. Click to expand... > Also your index needs to start at 0, not 1 Why? RBS
KMatle Expert Licensed User Longtime User Jun 8, 2020 #17 The loop ittself is ok but you can't set a byte to more than 127 RB Smissaert said: Could somebody tell why this fails? Code: Sub TestByteLoop Dim i As Byte Dim arrBytes(128) As Byte For i = 1 To 127 Log(i) arrBytes(i) = i '<< how can i go past 127 here? Log(arrBytes(i)) 'If i = 127 Then Exit 'this fixes it, but why is it needed? Next End Sub RBS Click to expand... The loop is ok but you can't store a value >127 in a byte. So when you need to fill all 255 possible values make a loop from 0 to 254 and subtract 127 from i. Vice versa add 127 to the value to get unsigned bytes. You can create a byte array as long as you want but the VALUES in it can be from -127 to +127. 127+127+0=255 different values unsigned. Upvote 0
The loop ittself is ok but you can't set a byte to more than 127 RB Smissaert said: Could somebody tell why this fails? Code: Sub TestByteLoop Dim i As Byte Dim arrBytes(128) As Byte For i = 1 To 127 Log(i) arrBytes(i) = i '<< how can i go past 127 here? Log(arrBytes(i)) 'If i = 127 Then Exit 'this fixes it, but why is it needed? Next End Sub RBS Click to expand... The loop is ok but you can't store a value >127 in a byte. So when you need to fill all 255 possible values make a loop from 0 to 254 and subtract 127 from i. Vice versa add 127 to the value to get unsigned bytes. You can create a byte array as long as you want but the VALUES in it can be from -127 to +127. 127+127+0=255 different values unsigned.
techknight Well-Known Member Licensed User Longtime User Jun 8, 2020 #18 RB Smissaert said: > Also your index needs to start at 0, not 1 Why? RBS Click to expand... because arrBytes(0) would be unused otherwise. Unless this is your intent. Without the context, I have no idea what youre trying to achieve. Upvote 0
RB Smissaert said: > Also your index needs to start at 0, not 1 Why? RBS Click to expand... because arrBytes(0) would be unused otherwise. Unless this is your intent. Without the context, I have no idea what youre trying to achieve.
R RB Smissaert Well-Known Member Licensed User Longtime User Jun 8, 2020 #19 techknight said: because arrBytes(0) would be unused otherwise. Unless this is your intent. Without the context, I have no idea what youre trying to achieve. Click to expand... I copied it from a bit of real code where array element 0 needed to be untouched. The Sub as posted doesn't do anything and all the array elements remain unused. RBS Upvote 0
techknight said: because arrBytes(0) would be unused otherwise. Unless this is your intent. Without the context, I have no idea what youre trying to achieve. Click to expand... I copied it from a bit of real code where array element 0 needed to be untouched. The Sub as posted doesn't do anything and all the array elements remain unused. RBS