1

I apologize if this is a very basic logic questions, but I saw some code in an example and I'm not understanding the logic structure, so I was hoping someone could explain it to me.

In my examples I'm using [Long] simply as a representation for any Long data type object


The example looks like this:
NOTE: wHi, wLow, LoLo, HiLo, LoHI, and HiHI are all long variables.

    If (wHi And &H8000&) Then
        MAKELONG = (((wHi And &H7FFF&) * 65536) Or (wLow And &HFFFF&)) Or &H80000000
    Else
        MAKELONG = LoLO Or (&H10000 * LoHI)
        'MAKELONG = ((wHi * 65535) + wLow)
    End If

I'm not sure what condition is being tested here: if [long] and [long] then and if [long] or [long] then

I also don't understand the return logic, e.g.: [LONG] = ([LONG] And [LONG]) or [LONG]


I tried playing in the immediate window and that did not shed any light on it for me as I did not see any obvious pattern:

?1 or 10
 11 
?2 or 10
 10 
?3 or 10
 11 
?4 or 10
 14 
? 1 and 10
 0 
?2 and 10
 2 
?3 and 10
 2 
?4 and 10
 0 
?5 and 10
 0 
CBRF23
  • 1,340
  • 1
  • 16
  • 44
  • 1
    it's bit-masking: http://stackoverflow.com/questions/4046457/how-can-i-do-a-bitwise-and-operation-in-vb-net – Marc B Nov 10 '14 at 21:25
  • http://en.wikipedia.org/wiki/Boolean_algebra – Steve Nov 10 '14 at 21:26
  • Turn `Option Strict On` and you will see that you are getting an error. It makes no sense to use a Long value in an If statement which expects a Boolean. – Chris Dunaway Nov 11 '14 at 15:24

1 Answers1

1

It's bit masking. e.g. With your test values:

    ?1 or 10 -> 11

      00000001 = binary for 1
      00001010 = binary for 10
   OR --------
      00001011 = binary for 11
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • I actually found the Wikipedia article for bit masking and bitwise operations pretty informative. http://en.wikipedia.org/wiki/Bitwise_operation – CBRF23 Nov 11 '14 at 14:18