PDA

View Full Version : AND operator in VBA



Kris Kurek
09-19-2018, 09:15 AM
I will appreciate it very much if somebody can instruct me how does AND operator work in the following code
wyn1 shows 92 - where does it come from - I can't understand

Thanks in advance :yes

Sub tst()
Dim wyn As Long, wyn1 As Long

wyn = 167772 / 256 ^ 0
wyn1 = wyn And 255
Debug.Print wyn1


End Sub

daSpud
09-19-2018, 09:30 AM
I will appreciate it very much if somebody can instruct me how does AND operator work in the following code
wyn1 shows 92 - where does it come from - I can't understand

Thanks in advance :yes

Sub tst()
Dim wyn As Long, wyn1 As Long

wyn = 167772 / 256 ^ 0
wyn1 = wyn And 255
Debug.Print wyn1
End Sub

167772 in binary is 0010 1000 1111 0101 1100 (hex 00028f5c) . when that is logically "anded" with 255 (in binary 0000 0000 1111 1111) you should get 92 (0000 0000 0101 1100) or hex 5c.

basically you are asking for the remainder of a number divided by 256.

the AND compares each bit in one number with the corresponding bit in the other number. If both are "1" then the result is "1" otherwise the result is "0". 255 is 8 bits of "1"s.

Kris Kurek
09-19-2018, 10:10 AM
Thanks a lot - Now I've understood - it is quite simple - A similar code is used to convert colors coded with decimal numbers into RGB notation, also with decimal numbers. For example the array below shows 92, 143 and 2 for RGB respectively calculated from ColorVal = 167772. To use the color conversion in the future I need to know the rules (Algorithm?), which was so simply explained by you, thanks again

DECIMAL2RGB = Array(ColorVal \ 256 ^ 0 And 255, ColorVal \ 256 ^ 1 And 255, ColorVal \ 256 ^ 2 And 255)

Kris Kurek
09-19-2018, 10:12 AM
167772 in binary is 0010 1000 1111 0101 1100 (hex 00028f5c) . when that is logically "anded" with 255 (in binary 0000 0000 1111 1111) you should get 92 (0000 0000 0101 1100) or hex 5c.

basically you are asking for the remainder of a number divided by 256.

the AND compares each bit in one number with the corresponding bit in the other number. If both are "1" then the result is "1" otherwise the result is "0". 255 is 8 bits of "1"s.

Thanks a lot - Now I've understood - it is quite simple - A similar code is used to convert colors coded with decimal numbers into RGB notation, also with decimal numbers. For example the array below shows 92, 143 and 2 for RGB respectively calculated from ColorVal = 167772. To use the color conversion in the future I need to know the rules (Algorithm?), which was so simply explained by you, thanks again

DECIMAL2RGB = Array(ColorVal \ 256 ^ 0 And 255, ColorVal \ 256 ^ 1 And 255, ColorVal \ 256 ^ 2 And 255)

daSpud
09-19-2018, 10:30 AM
That is why the ^0 in the expression you posted! I wondered because it was totally not necessary for 0 but very important for the other bytes of the color.

Kris Kurek
09-19-2018, 11:25 AM
Yes it was not relevant - the most important thing for me was to understand the AND "role" in the expression
Referring to your note: basically you are asking for the remainder of a number divided by 256. - the same result we can get after MOD(167772;256) = 92