I was debugging an old piece of code developed in VB6 and found something very strange. It can be demonstrated with the following simple code:
Private Sub Command1_Click()
Dim a As Integer
Dim b As Integer
Dim c As Integer
a = 0
Text1.Text = a - 1
Text2.Text = CStr((a - 1) Mod 4)
b = 0
b = b - 1
Text3.Text = b
Text4.Text = CStr(b Mod 4)
c = 0 - 1
Text5.Text = c
Text6.Text = CStr(c Mod 4)
End Sub
Here's what the form looked like:
You would think after the button is clicked Text2, Text4 and Text6 should show the same content, which is -1. That is the case when I pressed F5 to run it in the IDE.
This is what it looked like when I ran it in IDE:
The strange thing happened when I made an exe from IDE and ran the exe itself. Text1 and Text6 showed -1 but Text4 showed 3.
This is what it looked like when I ran the exe outside of IDE:
This happens only if the second operand is a power of 2. When I changed the 4's to 5, all the text boxes showed -1.
I tested this on 2 Windows 10 machines and got the same result.
I know VB6 is old and not many people still have it available now for test. I appreciate it if someone can help me understand this.
Thanks.