dsb has asked for the wisdom of the Perl Monks concerning the following question:
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:
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.print 155 | 105, "\n"; # prints 251\n # 1001 1011 - 155 # 0110 1001 - 105 # 1111 1011 - 251
As for the & operator, after doing the same sort of experimenting, I can see what's going on there as well.
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.print 155 & 105, "\n"; # prints 9\n print "JA" & "PH", \n"; # prints @@\n # 1001 1011 - 155 # 0110 1001 - 105 # 0000 1001 - 9
What is puzzling me is the ^ operator:
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 "j p \n " ^ " a h"; # prints JAPH\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.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
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 | |
Re: Bitwise Operators - TT&^/TT
by TheoPetersen (Priest) on Jul 25, 2001 at 00:55 UTC |