Page 49 - Open Soource Technologies 304.indd
P. 49

Unit 2: Language Basics



                                                                                                  Notes
            Less than or equal to (<=)
            If the left-hand operator is less than or equal to the right-hand operator, this operator returns
            true; otherwise, it returns false.
            Bitwise Operators
            The bitwise operators act on the binary representation of their operands. Each operand is first
            turned into a binary representation of the value, as described in the bitwise negation operator
            entry in the following list. All the bitwise operators work on numbers as well as strings, but they
            vary in their treatment of string operands of different lengths. The bitwise operators are:
            Bitwise negation (~)

            The bitwise negation operator changes 1s to 0s and 0s to 1s in the binary representations of the
            operands. Floating-point values are converted to integers before the operation takes place. If the
            operand is a string, the resulting value in a string of the same length as the original, with each
            character in the string negated.
            Bitwise AND (&)
            The bitwise AND operator compares each corresponding bit in the binary representations of the
            operands. If both bits are 1, the corresponding bit in the result is 1; otherwise, the corresponding
            bit is 0. For example, 0755 & 0671 is 0651. This is a bit easier to understand if we look at the binary
            representation. Octal 0755 are binary 111101101, and octal 0671 is binary 110111001. We can then
            easily see which bits are on in both numbers and visually come up with the answer:
                                111101101 & 110111001 ------------- 110101001
            The binary number 110101001 is octal 0651. You can use the PHP functions bindec( ), decbin( ),
            octdec( ), and decoct( ) to convert numbers back and forth when you are trying to understand
            binary arithmetic.
            Here’s a tip: Split the binary number up into three groups. 6 is binary 110, 5 is binary 101, and 1
            is binary 001; thus, 0651 is 110101001.

            If both operands are strings, the operator returns a string in which each character is the result of a
            bitwise AND operation between the two corresponding characters in the operands. The resulting
            string is the length of the shorter of the two operands; trailing extra characters in the longer string
            are ignored. For example, “wolf” & “cat” is “cad”.
            Bitwise OR (|)

            The bitwise OR operator compares each corresponding bit in the binary representations of the
            operands. If both bits are 0, the resulting bit is 0; otherwise, the resulting bit is 1. For example,
            0755 | 020 is 0775.

            If both operands are strings, the operator returns a string in which each character is the result of
            a bitwise OR operation between the two corresponding characters in the operands. The resulting
            string is the length of the longer of the two operands, and the shorter string is padded at the end
            with binary 0s. For example, “pussy” | “cat” is “suwsy”.

            Bitwise XOR (^)
            The bitwise XOR operator compares each corresponding bit in the binary representation of the
            operands. If either of the bits in the pair, but not both, is 1, the resulting bit is 1; otherwise, the
            resulting bit is 0. For example, 0755 ^ 023 is 776.
            If both operands are strings, this operator returns a string in which each character is the result of
            a bitwise XOR operation between the two corresponding characters in the operands. If the two


                                             LOVELY PROFESSIONAL UNIVERSITY                                    43
   44   45   46   47   48   49   50   51   52   53   54