BillKSmith
Veteran
Oct 17, 2013, 7:37 AM
Post #2 of 6
(4151 views)

AND, OR, and XOR are logical operators. They all take two arguments each of which has the value 'TRUE' or 'FALSE'. AND returns TRUE if both arguments are TRUE OR returns TRUE if at least one of the arguments is TRUE XOR returns TRUE if either argument is TRUE and the other is FALSE. Perl defines two symbols for each of these (They differ only in presidence.) Perl also defines three "Bitwise Operators". Their exact behavior depends on whether the arguments are numbers or strings. Your question seems to deal only with numbers. Inside the computer, numbers are stored in base two. (Only the 'digits' 0 and 1 are used). Perl allows base 8 (octal) and base 16 (Hex) as 'shorthand' way to write binary numbers. The operators do thir usual thing on each bit position. (One is TRUE, zero is FALSE) Lets look at your example of exclusive or. I assume that you intend your numbes to be binary.
Arg1: 11010101 Arg2: 10101010 Result: 01111111 In the first column, both arguments are true so the result is false. In all the other columns, only one of the arguments is true, so the result is true. Part of your confusion is that atleast in Perl, they symbol '&' is not the symbol for bitwise or. It is the symbol for bitwise and. Repeating your example for and we see:
Arg1: 11010101 Arg2: 10101010 Result: 10000000 In the code you show, the variable $mode probably contains other information in the high order bits. The & 07777 discards all but the last twelve bits which are enough to hold mode numbers as large as 4095. Good Luck, Bill
