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


In reply to Bitwise Operators - &^ by dsb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.