05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Bitwise Operators<br />

The bitwise operators act on the binary representation of their operands. Each operand<br />

is first turned into a binary representation of the value, as described in the bitwise<br />

negation operator entry in the following list. All the bitwise operators work on<br />

numbers as well as strings, but they vary in their treatment of string operands of different<br />

lengths. The bitwise operators are:<br />

Bitwise negation (~)<br />

The bitwise negation operator changes 1s to 0s and 0s to 1s in the binary representations<br />

of the operands. Floating-point values are converted to integers before<br />

the operation takes place. If the operand is a string, the resulting value is a string<br />

the same length as the original, with each character in the string negated.<br />

Bitwise AND (&)<br />

The bitwise AND operator compares each corresponding bit in the binary representations<br />

of the operands. If both bits are 1, the corresponding bit in the result<br />

is 1; otherwise, the corresponding bit is 0. For example, 0755 & 0671 is 0651. This<br />

is a bit easier to understand if we look at the binary representation. Octal 0755 is<br />

binary 111101101, and octal 0671 is binary 110111001. We can the easily see<br />

which bits are on in both numbers and visually come up with the answer:<br />

111101101<br />

& 110111001<br />

---------<br />

110101001<br />

The binary number 110101001 is octal 0651. * You can use the <strong>PHP</strong> functions<br />

bindec( ), decbin( ), octdec( ), and decoct( ) to convert numbers back and forth<br />

when you are trying to understand binary arithmetic.<br />

If both operands are strings, the operator returns a string in which each character<br />

is the result of a bitwise AND operation between the two corresponding characters<br />

in the operands. The resulting string is the length of the shorter of the two<br />

operands; trailing extra characters in the longer string are ignored. For example,<br />

"wolf" & "cat" is "cad".<br />

Bitwise OR (|)<br />

The bitwise OR operator compares each corresponding bit in the binary representations<br />

of the operands. If both bits are 0, the resulting bit is 0; otherwise, the<br />

resulting bit is 1. For example, 0755 | 020 is 0775.<br />

If both operands are strings, the operator returns a string in which each character<br />

is the result of a bitwise OR operation between the two corresponding characters<br />

in the operands. The resulting string is the length of the longer of the two<br />

operands, and the shorter string is padded at the end with binary 0s. For example,<br />

"pussy" | "cat" is "suwsy".<br />

* Here’s a tip: split the binary number up into three groups. 6 is binary 110, 5 is binary 101, and 1 is binary<br />

001; thus, 0651 is 110101001.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Expressions and Operators | 41

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!