Consulting

Results 1 to 6 of 6

Thread: AND operator in VBA

  1. #1
    VBAX Regular
    Joined
    Sep 2018
    Location
    Polska
    Posts
    9
    Location

    AND operator in VBA

    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

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

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


    End Sub

  2. #2
    VBAX Regular
    Joined
    Apr 2018
    Posts
    10
    Location
    Quote Originally Posted by Kris Kurek View Post
    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

    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.
    Last edited by daSpud; 09-19-2018 at 09:41 AM.

  3. #3
    VBAX Regular
    Joined
    Sep 2018
    Location
    Polska
    Posts
    9
    Location
    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)

  4. #4
    VBAX Regular
    Joined
    Sep 2018
    Location
    Polska
    Posts
    9
    Location
    Quote Originally Posted by daSpud View Post
    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)

  5. #5
    VBAX Regular
    Joined
    Apr 2018
    Posts
    10
    Location
    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.

  6. #6
    VBAX Regular
    Joined
    Sep 2018
    Location
    Polska
    Posts
    9
    Location
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •