Announcement

Collapse
No announcement yet.

AND logical operator

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • AND logical operator

    Can someone pls explain how this works?
    IF (WFD.dwFileAttributes AND 16) = 0 THEN

    in
    Code:
        DO
            IF (WFD.dwFileAttributes AND 16) = 0 THEN  ' Exclude dirs 
                INCR Cnt
                REDIM PRESERVE Fi(Cnt)
                Fi(Cnt) = RTRIM$(wfd.cFileName, ANY CHR$(0,32))
            END IF
            IF ISFALSE FINDNEXTFILE(hFind, wfd) THEN CALL FINDCLOSE(hFind) : EXIT DO
        LOOP
    How can you test "16"?
    Obviously it is never equal to 0 so this should allways fail
    yet it doesnt.
    I dont get it.

    ------------------
    Kind Regards
    Mike

  • #2
    The parentheses tell PowerBASIC to perform a bitwise AND operation.

    So that expression tests for whether or not "the 16 bit" (i.e. bit #4) is set in WFD.dwFileAttributes.

    Code:
    Bit #    Value
     0         1
     1         2
     2         4
     3         8
     4        16
     5        32
     6        64
     7       128
     8       256
    etc.
    If WFD.dwFileAttributes has a value that has the 16 bit set, that expression will return 1. Otherwise it will return zero.
    -- Eric


    ------------------
    Perfect Sync Development Tools
    Perfect Sync Web Site
    Contact Us: mailto:[email protected][email protected]</A>



    [This message has been edited by Eric Pearson (edited August 01, 2001).]
    "Not my circus, not my monkeys."

    Comment


    • #3
      Eric,

      thx, dont you mean NOT set, cos its testing = 0

      and how do you translate: file to Bit #4

      Here is what WinHlp says, but no reference to Bits ???

      Specifies the file attributes of the file found. This member can be one or more of the following values:

      Value Meaning
      FILE_ATTRIBUTE_ARCHIVE
      The file is an archive file. Applications use this value to mark files for backup or removal.
      FILE_ATTRIBUTE_COMPRESSED
      The file or directory is compressed. For a file, this means that all of the data in the file is compressed. For a directory, this means that compression is the default for newly created files and subdirectories.
      FILE_ATTRIBUTE_DIRECTORY
      The file is a directory.
      FILE_ATTRIBUTE_HIDDEN
      The file is hidden. It is not included in an ordinary directory listing.
      FILE_ATTRIBUTE_NORMAL
      The file has no other attributes set. This value is valid only if used alone.
      FILE_ATTRIBUTE_OFFLINE
      The data of the file is not immediately available. Indicates that the file data has been physically moved to offline storage.
      FILE_ATTRIBUTE_READONLY
      The file is read-only. Applications can read the file but cannot write to it or delete it.
      FILE_ATTRIBUTE_SYSTEM
      The file is part of the operating system or is used exclusively by it.
      FILE_ATTRIBUTE_TEMPORARY
      The file is being used for temporary storage.

      What do these all translate to in Bits?

      ------------------
      Kind Regards
      Mike

      Comment


      • #4
        In binary world, everything is bits. We really only have 0 and 1 to use,
        but today's compilers translate the bits to decimal system. This is why
        it's faster to do bitwise compare - (almost) no need to translate anything.

        Some people use the BIT() functions, but my own tests have show them to
        be much slower than simple AND operator against mask, within paranthesis.

        In this case, one must always test the bits. A folder can have several bits
        (flags) set, like hidden, etc, so decimal value becomes 18 (whatever), but it
        will always have at least bit 4 (=16) set. If bit 4 is zero, it is
        a file (unless MS has come up with some new "invention" lately..)

        To confuse things even more - values are usually given in hexadecimal form,
        where H10 equals 16 (in HEX world, 0123456789ABCDEF). From latest win32api,inc:
        Code:
        %FILE_ATTRIBUTE_READONLY                     = &H1
        %FILE_ATTRIBUTE_HIDDEN                       = &H2
        %FILE_ATTRIBUTE_SYSTEM                       = &H4
        %FILE_ATTRIBUTE_DIRECTORY                    = &H10
        %FILE_ATTRIBUTE_ARCHIVE                      = &H20
        %FILE_ATTRIBUTE_NORMAL                       = &H80
        %FILE_ATTRIBUTE_TEMPORARY                    = &H100
        %FILE_ATTRIBUTE_COMPRESSED                   = &H800
        Now, if this wasn't clear as mud, I don't know what is..

        (imagine the old days, when they had to use mechanical switches to set
        each individual bit in order to create a running program. Now, that is
        what I call programming..
        ------------------
        PS: I like the way MS describes a directory: "then file is a directory"..
        Then horse is an elephant? Car is a boat? ..?

        [This message has been edited by Borje Hagsten (edited August 01, 2001).]

        Comment


        • #5
          Code:
          16 in "bits" (read as binary) is 000000000010000 (hex &h10)
          
                                         Decimal       Hex       Binary
          %FILE_ATTRIBUTE_READONLY       = 1           = &H1     = 0000000000000001
          %FILE_ATTRIBUTE_HIDDEN         = 2           = &H2     = 0000000000000010
          %FILE_ATTRIBUTE_SYSTEM         = 4           = &H4     = 0000000000000100
          %FILE_ATTRIBUTE_DIRECTORY      = 16          = &H10    = 0000000000010000
          %FILE_ATTRIBUTE_ARCHIVE        = 32          = &H20    = 0000000000100000
          %FILE_ATTRIBUTE_NORMAL         = 128         = &H80    = 0000000010000000
          %FILE_ATTRIBUTE_TEMPORARY      = 256         = &H100   = 0000000100000000
          %FILE_ATTRIBUTE_COMPRESSED     = 2048        = &H800   = 0000100000000000
          
          The AND operation checks bit for bit.  If BOTH bits are 1 then the final results is one all other cases are 0.
          
          Example: if you AND the decimal numbers 186 and 16
          
          186 in Binary is 0000000010111010
          16  in binary is 0000000000010000
          =================================   (perform AND operation)
                           0000000000010000   The answer is 16
          
                                      ^
                                      |____ see how the 1's line up for bit 4
          
          
          Bits are numbered from right to left starting at 0.
          
          see the numbering example below.  
          
          1111110000000000    (bit numbering
          5432109876543210     chart)
          
          0000000010111010     Decimal 186 (hex &hba)
                  ^  ^
                  |  |___bit 4
                  |
                  |___bit 7

          ------------------
          George W. Bleck
          Senior System Engineer
          KeySpan Corporation


          [This message has been edited by George Bleck (edited August 01, 2001).]
          <b>George W. Bleck</b>
          <img src='http://www.blecktech.com/myemail.gif'>

          Comment


          • #6
            Thank you for such an excellent explanation guys!

            ------------------
            Kind Regards
            Mike

            Comment


            • #7
              Mike, you may also wish to read the section on Short-Circuit evaluation in the IF/THEN BLOCK STATEMENT section of the help file.


              ------------------
              Lance
              PowerBASIC Support
              mailto:[email protected][email protected]</A>
              Lance
              mailto:[email protected]

              Comment


              • #8
                Lance, I've seen an explanation for short-circuiting in these
                forums. However, I can't find the Short-Circuit section that you
                are referring to in the help file. My PB/DLL help file is dated
                7/2/99. Do I have the latest one?

                By the way, in the help file, there's a combo-box, but it always
                seems to be empty (other than the text bar). It would be nice
                for it to list previous search words like MSDN does.

                ------------------
                Daniel Corbier
                UCalc Fast Math Parser
                http://www.ucalc.com
                Daniel Corbier
                uCalc Fast Math Parser
                uCalc Language Builder
                sigpic

                Comment


                • #9
                  My mistake. The section on Short-Circuit evaluation is in the printed documentation only.

                  Combobox? Do you mean the edit box in the Index Tab or on the Search Tab?

                  ------------------
                  Lance
                  PowerBASIC Support
                  mailto:[email protected][email protected]</A>
                  Lance
                  mailto:[email protected]

                  Comment


                  • #10
                    Lance, I'm referring to the one in the search tab.

                    ------------------
                    Daniel Corbier
                    UCalc Fast Math Parser
                    http://www.ucalc.com
                    Daniel Corbier
                    uCalc Fast Math Parser
                    uCalc Language Builder
                    sigpic

                    Comment


                    • #11
                      The dropdown list in the search tab is controlled by Robohelp. I don't believe that antries are preserved between launches of the help file, but I'll ask the documentation department to look into it.

                      ------------------
                      Lance
                      PowerBASIC Support
                      mailto:[email protected][email protected]</A>
                      Lance
                      mailto:[email protected]

                      Comment

                      Working...
                      X