how to use compare function
-
dear all how to use compare function, the following is the example code for reading input bit
Dim iShift As Double, I As Double
iShift = 1 For I = 0 To 7 Compare = Int(InputByte And iShift) If Compare = iShift Then 'Bit Turn OFF AMD\_GalilInputAxisBit(I) = 1 AMD\_GalilInputBit(I) = 1 'DigitalIO.Label2(I).BackColor = vbRed Else AMD\_GalilInputAxisBit(I) = 0 'Bit Turn ON AMD\_GalilInputBit(I) = 0 'DigitalIO.Label2(I).BackColor = vbGreen End If iShift = iShift \* 2 Next I
-
dear all how to use compare function, the following is the example code for reading input bit
Dim iShift As Double, I As Double
iShift = 1 For I = 0 To 7 Compare = Int(InputByte And iShift) If Compare = iShift Then 'Bit Turn OFF AMD\_GalilInputAxisBit(I) = 1 AMD\_GalilInputBit(I) = 1 'DigitalIO.Label2(I).BackColor = vbRed Else AMD\_GalilInputAxisBit(I) = 0 'Bit Turn ON AMD\_GalilInputBit(I) = 0 'DigitalIO.Label2(I).BackColor = vbGreen End If iShift = iShift \* 2 Next I
What are you talking about? You're not using a Compare method or function at all in this code. All you're doing is comparing a numeric value to another one.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak -
dear all how to use compare function, the following is the example code for reading input bit
Dim iShift As Double, I As Double
iShift = 1 For I = 0 To 7 Compare = Int(InputByte And iShift) If Compare = iShift Then 'Bit Turn OFF AMD\_GalilInputAxisBit(I) = 1 AMD\_GalilInputBit(I) = 1 'DigitalIO.Label2(I).BackColor = vbRed Else AMD\_GalilInputAxisBit(I) = 0 'Bit Turn ON AMD\_GalilInputBit(I) = 0 'DigitalIO.Label2(I).BackColor = vbGreen End If iShift = iShift \* 2 Next I
Hi, seems acceptable; here are a few remarks: 1. when manipulating bits, make sure your variables are integers, so don't use double. 2. while your comparison to iShift is OK, you could as well test for zero. 3. maybe, just maybe, you have your two cases (equal and not equal) swapped. This would be my style (assuming InputByte is an integer too):
Dim I As Integer Dim iShift As Integer = 1 For I = 0 To 7 If (InputByte And iShift)= 0 Then 'Bit Turn OFF AMD\_GalilInputAxisBit(I) = 0 AMD\_GalilInputBit(I) = 0 Else 'Bit Turn ON AMD\_GalilInputAxisBit(I) = 1 AMD\_GalilInputBit(I) = 1 End If iShift = iShift + iShift Next I
:)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.