Nov 26, 2017 Replies: 6 Here's a reminder that we don't need an extra variable for swapping integer values: Code: Copy Dim a = 120, b = 110 As Byte 'Short / Int / Long Log(a & " | " & b) 'Output: 120 | 110 a = a + b b = a - b a = a - b Log(a & " | " & b) 'Output: 110 | 120 It works for doubles and floats as well, but you might lose precision (12.2 becomes 12.1999998), so beware. Click to expand...
Here's a reminder that we don't need an extra variable for swapping integer values: Code: Copy Dim a = 120, b = 110 As Byte 'Short / Int / Long Log(a & " | " & b) 'Output: 120 | 110 a = a + b b = a - b a = a - b Log(a & " | " & b) 'Output: 110 | 120 It works for doubles and floats as well, but you might lose precision (12.2 becomes 12.1999998), so beware.
Emme Developer Well-Known Member Licensed User Longtime User Nov 27, 2017 #2 Another way Code: Copy Dim a = 120, b = 110 As Byte 'Short / Int / Long Log(a & " | " & b) 'Output: 120 | 110 a = Bit.Xor(a,b) b = Bit.Xor(a,b) a = Bit.Xor(a,b) Log(a & " | " & b) 'Output: 110 | 120
Another way Code: Copy Dim a = 120, b = 110 As Byte 'Short / Int / Long Log(a & " | " & b) 'Output: 120 | 110 a = Bit.Xor(a,b) b = Bit.Xor(a,b) a = Bit.Xor(a,b) Log(a & " | " & b) 'Output: 110 | 120
wonder Expert Licensed User Longtime User Nov 28, 2017 #3 Emme Developer said: Another way Code: Copy Dim a = 120, b = 110 As Byte 'Short / Int / Long Log(a & " | " & b) 'Output: 120 | 110 a = Bit.Xor(a,b) b = Bit.Xor(a,b) a = Bit.Xor(a,b) Log(a & " | " & b) 'Output: 110 | 120 Click to expand... Can you use it for floats and doubles without loosing precision? I have no access to B4X right now.
Emme Developer said: Another way Code: Copy Dim a = 120, b = 110 As Byte 'Short / Int / Long Log(a & " | " & b) 'Output: 120 | 110 a = Bit.Xor(a,b) b = Bit.Xor(a,b) a = Bit.Xor(a,b) Log(a & " | " & b) 'Output: 110 | 120 Click to expand... Can you use it for floats and doubles without loosing precision? I have no access to B4X right now.
Emme Developer Well-Known Member Licensed User Longtime User Nov 28, 2017 #4 wonder said: Can you use it for floats and doubles without loosing precision? I have no access to B4X right now. Click to expand... Code: Copy Dim a = 120.2, b = 110.7 As Float 'Short / Int / Long Log(a & " | " & b) 'Output: 120.19999694824219 | 110.69999694824219 a = Bit.Xor(a,b) b = Bit.Xor(a,b) a = Bit.Xor(a,b) Log(a & " | " & b) 'Output: 110 | 120 This is the log, bit.xor return an int so you lost the decimal values
wonder said: Can you use it for floats and doubles without loosing precision? I have no access to B4X right now. Click to expand... Code: Copy Dim a = 120.2, b = 110.7 As Float 'Short / Int / Long Log(a & " | " & b) 'Output: 120.19999694824219 | 110.69999694824219 a = Bit.Xor(a,b) b = Bit.Xor(a,b) a = Bit.Xor(a,b) Log(a & " | " & b) 'Output: 110 | 120 This is the log, bit.xor return an int so you lost the decimal values
wonder Expert Licensed User Longtime User Nov 28, 2017 #5 Ahh... if I remember, the arguments for Bit.Xor() are integers, right?
Emme Developer Well-Known Member Licensed User Longtime User Nov 28, 2017 #6 wonder said: Ahh... if I remember, the arguments for Bit.Xor() are integers, right? Click to expand... Yes, also the value got from Xor is an Int
wonder said: Ahh... if I remember, the arguments for Bit.Xor() are integers, right? Click to expand... Yes, also the value got from Xor is an Int
Emme Developer Well-Known Member Licensed User Longtime User Nov 28, 2017 #7 Using strings Code: Copy Dim a = "A", b = "B" As String Log(a & " | " & b) 'Output: A | B a = A&B b = A.replace(B,"") a = A.replace(B,"") Log(a & " | " & b) 'Output: B | A
Using strings Code: Copy Dim a = "A", b = "B" As String Log(a & " | " & b) 'Output: A | B a = A&B b = A.replace(B,"") a = A.replace(B,"") Log(a & " | " & b) 'Output: B | A