Home > PHP > Questions on Bitwise Operators in PHP

Questions on Bitwise Operators in PHP

Q:- What do you mean by bitwise operators?
Ans: The bitwise operators act on the binary representation on their operands. Each operand is first turned into a binary representation of the value before operation.

Q:- Do these operators work only on numbers?
Ans: No, they work for numbers as well as strings but they vary in their treatment of string operands of different lengths.

Q:- What is bitwise negation(~)?
Ans: It changes 1s to 0s and 0s to 1s in the binary representations of the operands. For string operands, the resulting value is a string of same length, with each character in the string negated.

Q:- What is bitwise AND(&)?
Ans: It compares the corresponding bits of both the operands and returns 1 if both bits are 1 otherwise returns 0.
Example: 0755 & 0671
octal(0755) -> 111101101
&
octal(0671) -> 110111001

———————————

110101001 -> octal(0651)

so output is 0651. If both the operands are strings, then the resulting string is the length of shorter of the two operands with the bitwise AND operation between the two corresponding characters of operands; the trailing extra characters in the longer string are ignored. Example: “wolf” & “cat” is “cad”

Q:- What is bitwise OR(|)?
Ans:It compares the corresponding bits of both the operands and returns 0 if both bits are 0 otherwise returns 1.
Example: 0755|020 is 0775
If both the operands are strings, then the resulting string is the length of longer of the two operands with the bitwise OR operation between the two corresponding characters of operands; and the shorter string is padded at the end with binary 0s.
Example: “pussy” | “cat” is “suwsy”.

Q:- Say something about bitwise XOR(^).
Ans: It compares the corresponding bits of both the operands and returns 1 if either of the bits in pair, but not both, is 1, otherwise it returns 0.
Example: 0755 ^ 023 is 776
If both the operands are strings, then the resulting string is the length of shorter of the two operands with the bitwise XOR operation between the two corresponding characters of operands. The extra trailing characters in the longer string are ignored.
Example: “big drink” | “AA” is “#(“.

Q:- What is Left shift(<<) operator?
Ans. This operator shifts the bits in the binary representation of the left hand operand left by the number of places given in the right-hand operand. Shifting a binary number to the left inserts a 0 as the rightmost bit of the number and moves all other bits to the left one place.
Example: 3<<1 is 6 (binary(3)->11 shifted one place left results 110 i.e 6).
The result of left shifting is multiplying the left-hand operand by 2 to the power of the right-hand operand.

Q:- What is right shift(>>) operator?

Ans: This operator shifts the bits in the binary representation of the left hand operand right by the number of places given in the righthand operand. Shifting a binary number to the right inserts a 0 as the leftmost bit of the number and moves all other bits to the right one place. The rightmost bit is discarded.Example: 13 >> 1 is 6.

Advertisements
Categories: PHP
  1. mini
    July 13, 2006 at 6:11 am

    These idea regarding binary operators are really nice, anyone can understand it easily.Good, keep it up.

  2. July 13, 2006 at 6:57 am

    Thanks Mini! I like some suggestions like any improvement needed, how I can be more informative etc. But thanks anyway, I will try to be more specific.
    Thanks.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: