Jump to content

help with bitAND, bitOR


 Share

Go to solution Solved by Gianni,

Recommended Posts

  • Solution

@SadBunny, thank you for the explanation. I don't understand it right now, but as I experiement more I'll keep coming back to your comment and hopefully it will click eventually.

Re: your post about the comments, using the word 'between' was probably not the best.

For the first code example I'll explain what I mean.

; write to console =  1, 3, 5, 7

This means that the function should 'write to console' IF the result of the BitAND(or BitOR???) would equal 1, 3, 5, or 7.

console = 1

file = 2

eventlog = 4

so (console = 1), (console + file = 3), (console + eventlog = 5), (console + file + eventlog = 7)

At least that's how I understand BitAND's to work?

 

I try to explain how BitAnd is used, and I hope to not confuse you even more....

in that case forget all what follw (also, hope that I have not made mistakes).

To understand how the operators BitAnd and BitOr works, is required a brief review of binary numbers

conversion table from a 4 bits binary number to corresponding decimal number.

(sum the decimal values on top of the binary numbers on the table where there

 there is an 1 in the binary number and you have the decimal value)

   8 4 2 1    decimal

   -------    -------

   0 0 0 0  =       0

   0 0 0 1  =       1

   0 0 1 0  =       2

   0 0 1 1  =       3

   0 1 0 0  =       4

   0 1 0 1  =       5

   0 1 1 0  =       6

   0 1 1 1  =       7

   1 0 0 0  =       8

   1 0 0 1  =       9

   1 0 1 0  =      10

   1 0 1 1  =      11

   1 1 0 0  =      12

   1 1 0 1  =      13

   1 1 1 0  =      14

   1 1 1 1  =      15

----------------------------------------------------------------------------------------

Synopsis about BitOR

--------------------

BitOr is used to turn On bits (pass an 1 on each position you want turn On)

so, if you have this binary number 1 0 0 1 (decimal 9)

                                       ^

                                       |

and you want turn on this bit  --------+

use this command BitOr(2,9) ---> BitOr(0010, 1001) ---> result is 1011 (decimal 11)

where you pass an 1 the target Bit is turned On

where you pass a 0 the target Bit remains unchanged

+-----------------------+

|   BitOr Truth table   |

+-------+-------+-------+

| INPUT | INPUT |  OUT  |

+-------+-------+-------+

|   0   |   0   |   0   |

+-------+-------+-------+

|   0   |   1   |   1   |

+-------+-------+-------+

|   1   |   0   |   1   |

+-------+-------+-------+

|   1   |   1   |   1   |

+-------+-------+-------+

----------------------------------------------------------------------------------------

Synopsis about BitAND

---------------------

BitAnd is used to turn Off bits (pass an 0 on each position you want turn Off)

so, if you have this binary number 1 0 1 1 (decimal 11)

                                         ^

                                         |

and you want turn off this bit  ---------+

use this command BitAnd(14, 11) ---> BitAnd(1110, 1011) ---> result is 1010 (decimal 10)

where you pass an 1 the target Bit remains unchanged

where you pass a 0 the target bit is turned Off

+-----------------------+

|  BitAnd Truth table   |

+-------+-------+-------+

| INPUT | INPUT |  OUT  |

+-------+-------+-------+

|   0   |   0   |   0   |

+-------+-------+-------+

|   0   |   1   |   0   |

+-------+-------+-------+

|   1   |   0   |   0   |

+-------+-------+-------+

|   1   |   1   |   1   |

+-------+-------+-------+

-----------------------------------------------------------------------------------------

Now, considering your example with the values 1 2 4, it should be quite easy to explain how those are managed by BitAND,

see the following table with decimal values on the left and corresponding binary values on the middle and meaning you give to

single bits (flags) on the right:

(each bit set to 1 in the binary is like a flag that says: print if is 1 or don't print if is 0)

decimal      binary       your meaning

            8 4 2 1

-------     -------        ------------

   1        0 0 0 1        1 = console

   2        0 0 1 0        2 = file

   4        0 1 0 0        4 = eventlog

   3        0 0 1 1        1 + 2     = console + file

   5        0 1 0 1        1 + 4     = console + eventlog

   7        0 1 1 1        1 + 2 + 4 = console + file + eventlog

              ^ ^ ^

              | | |

              | | +----> first column is a flag for console

              | |

              | +------> second column is a flag for file

              |

              +--------> third column is a flag for eventlog

so for example if you have a decimal number of 7 that is 0 1 1 1 in binary, it means:

0 1 1 1

  ^ ^ ^

  | | |

  | | +--> first column (first bit) is 1 so print to console (value of first bit converted to decimal equals to 1)

  | |

  | +----> second column (second bit) is 1 so print to file (value of second bit converted to decimal equals to 2)

  |

  +------> third column (third bit) is 1 so print to eventlog (value of third bit converted to decimal equals to 4)

meaning of decimal number 7 is print to console + file + eventlog (the above 3 bits turned on results in decimal 1 + 2 + 4 = 7)

How do you know where to print starting from the decimal number?

well, you have to check each bit of the first 3 columns of the (binary) number to see if are 1 or 0 and print accordingly,

Something as follows:

suppose now we have the number 5 that is 0101 in binary

you can easly see that it should print console + eventlog

but how to check it programmatically?

1) check first bit in first column (first bit on the right) to see if you have to print to the console,

   if first bit is 1 print to the console otherwise don't print to console

to see if a bit is 1 or 0 you have to use BitAnd to turn off all other bits but the one you want to check so that you can see the

state of that remaining bit (if result is 0 that bit was 0 if result is <> 0 that bit was 1..... quite easy)

to turn off bits you have to use the BitAND operator in this way

BitAND(1,5) ---> BitAnd(0001,  0101) ---> result 1 (since result is <> 0 it means that that bit was On)

If BitAnd(1,5) then PrintConsole()

BitAND turn off all bits where there is at least a bit 0 in one side of the 2 corresponding bits:

   now check first column (first bit)

BitAND (1 , 5) -->  1

        -   -

        1   1  -->  1

        0   0  -->  0

        0   1  -->  0

        0   0  -->  0

result is 1, this means that first bit was on so print to console

---------------------------

2) now check second column (second bit)

BitAND (2 , 5) -->  0

        -   -

        0   1  -->  0

        1   0  -->  0

        0   1  -->  0

        0   0  -->  0

result is 0, this means that second bit was off so don't print to file

---------------------------

3) now check third column (third bit)

BitAND (4 , 5) -->  4

        -   -

        0   1  -->  0

        0   0  -->  0

        1   1  -->  1

        0   0  -->  0

result is 4 (<> 0), this means that third bit was on so print to eventlog

---------------------------

now we put above concepts in your AutoIt script to check where to print using BitAND....:

; Indicates write method options

;                              4 2 1 <-- decimal values of binary bits below
;                             ------
Global Const $L_CONSOLE = 1 ;  0 0 1
Global Const $L_FILE = 2 ;     0 1 0
Global Const $L_EVENT = 4 ;    1 0 0

$iFlag = 4 ; <-- 4 means print only to event log (change this value to test the other combinations)


; write to console =  1, 3, 5, 7
; write to file = 2, 3, 6, 7
; write to evenlog = 4, 5, 6, 7

; If BitOR($iFlag, 3) Then ConsoleWrite("yes" & @CR) ; ???

; check if print to console (first bit)
If BitAND($iFlag, $L_CONSOLE) Then ConsoleWrite("Print to console" & @CR) ; BitAND(4, 1) --> BitAND(100, 001) --> result 000 (0)

; Check if print to file (second bit)
If BitAND($iFlag, $L_FILE) Then ConsoleWrite("Print to file" & @CR) ;       BitAND(4, 2) --> BitAND(100, 010) --> result 000 (0)

; check if print to event log (third bit)
If BitAND($iFlag, $L_EVENT) Then ConsoleWrite("Print to event log" & @CR) ; BitAND(4, 4) --> BitAND(100, 100) --> result 100 (4)
Edited by Chimp

 

image.jpeg.9f1a974c98e9f77d824b358729b089b0.jpeg Chimp

small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt....

Link to comment
Share on other sites

Thanks to all for everyones help.

Seeing code I better understand how to use the BitAND. It seems I was close, but not close enough.

Sorry for my absence I was out of town at a tech conference.  (hotels out there still exist that dont have free wifi)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...