Monday, November 28, 2011

Bit shifts or bitwise operations

Bit shifts:-The bit shifts are sometimes considered bitwise operations, since they operate on the binary representation of an integer instead of its numerical value;
The bit shifts operators are used to perform bitwise operations on the binary representation of an integer instead of its numerical value.

Sign Bit:-A sign bit is found in the left most position of the number and is know as most significant bit (MSB) which indicates the status of a number i.e. the number is positive or negative. If the value of the sign bit is 0 then the number is positive; otherwise the number is negative, if the value of the sign bit is 1.
I. Signed Left Shift ("<<") :-The signed left shift ("<<") operator shifts a bit (or bits) to the left by the distance specified in the right operand.
II. Signed Right Shift (">>") :-The signed right shift (">>") operator shifts a bit (or bits) to the right by the distance specified in the right operand and fills the left most bit by the sign bit.

III. Unsigned Right Shift (">>>"):-The unsigned right shift (">>>") operator behave like the signed right shift operator. i.e. it shifts a bit (or bits) to the right. But unlike ">>" operator, this operator always shifts zeros into the leftmost position by the distance specified in the right operand. So the result of applying the >>>operator is always positive.

For example, the expression "14>>>2"; shifts all bits of the number 14 to the right placing a zero to the left for each blank place Thus the value 1110 becomes 0011 or 3 in decimal.

No comments:

Post a Comment