dsb has asked for the wisdom of the Perl Monks concerning the following question:

While going through perlop, and reading specifically through the section on bitwise operators, I realized that I have a couple of questions.

I understand(for the most part anyway) the & and | operators. After looking at the binary values and the results of a couple of operations, I was able to see a couple of things:

print 155 | 105, "\n"; # prints 251\n # 1001 1011 - 155 # 0110 1001 - 105 # 1111 1011 - 251
When working on numeric values, the operator will examine the bits, and decimal value that is a binary number containing all the positive bits(1's). In this case, the only bit that is still negative(0) after combining both of the strings, is the 4 bit. That I get. I also get when string values are |'d.

As for the & operator, after doing the same sort of experimenting, I can see what's going on there as well.

print 155 & 105, "\n"; # prints 9\n print "JA" & "PH", \n"; # prints @@\n # 1001 1011 - 155 # 0110 1001 - 105 # 0000 1001 - 9
When the bits are &'d together, the value returned is the decimal or string value of the binary number that combines only common positive bits.

What is puzzling me is the ^ operator:

print 155 ^ 105, "\n"; # prints 242\n print "j p \n " ^ " a h"; # prints JAPH\n
At first a though that this operator sought out the bit value of all positive bits, and added them all up, but when 155 and 105 are XORed, that produces 240, not 242. Then I thought, maybe that common bits were counted twice each, so I tried these:
print 155 ^ 105, "\n"; # prints 242\n print 128 ^ 128, "\n"; # prints 0\n print 144 ^ 12, "\n"; # prints 156\n print 128 ^ 32, "\n"; # prints 160\n
The last two tries support my original theory, but the first don't. Needless to say I'm pretty confused, and if anyone could clear this up(or any other misconceptions for that matter), I'd appreciate it.

Amel - f.k.a. - kel

Replies are listed 'Best First'.
Re: Bitwise Operators - TT&^/TT
by HamNRye (Monk) on Jul 25, 2001 at 01:11 UTC

    The XOR operator:

    Compares two bits and returns a "1" if the operands are different.

    Here is the truth table for this:
    Bit 1Bit 2Result
    000
    011
    101
    110

    For example one:

    1001 1011 #155 0110 1001 #105 1111 0010 #242

    For example two:

    1000 0000 #128 1000 0000 #128 0000 0000 #0

    Hope this helps!
    ~Hammy

Re: Bitwise Operators - TT&^/TT
by TheoPetersen (Priest) on Jul 25, 2001 at 00:55 UTC
    but when 155 and 105 are XORed, that produces 240, not 242.

    Says who? The exclusive or of two bit patterns contains a one for each mismatch pair of bits and a zero for each matched pair. Thus for 155 and 105:

    # 1001 1011 - 155 # 0110 1001 - 105 # 1111 0010 - 242
    The fun thing about XOR is that if you XOR the result with one of the input values, you get the other input value back:
    # 1001 1011 - 155 # 1111 0010 - 242 # 0110 1001 - 105
    Thus various encryption schemes (and some fun graphics programming on early PCs :)