I have some VB script in classic ASP that looks like this:
if (x and y) > 0 then
'do something
end if
It seems to work like this: (46 and 1) = 0 and (47 and 1) = 1
I don't understand how that works. Can someone explain that?
It's a Bitwise AND.
47 is 101111
AND 1 is 000001
= 000001
while
46 is 101110
AND 1 is 000001
= 000000
It's doing a bitwise comparison -
Bitwise operations evaluate two integral values in binary (base 2) form. They compare the bits at corresponding positions and then assign values based on the comparison.
and a further example -
x = 3 And 5
The preceding example sets the value of x to 1. This happens for the following reasons:
The values are treated as binary:
3 in binary form = 011
5 in binary form = 101
The And operator compares the binary representations, one binary position (bit) at a time. If both bits at a given position are 1, then a 1 is placed in that position in the result. If either bit is 0, then a 0 is placed in that position in the result. In the preceding example this works out as follows:
011 (3 in binary form)
101 (5 in binary form)
001 (The result, in binary form)
The result is treated as decimal. The value 001 is the binary representation of 1, so x = 1.
From - http://msdn.microsoft.com/en-us/library/wz3k228a(v=vs.80).aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With