Subramaniam Govindasamy

Greenhorn

Posts: 1

posted 12 years ago

First of all...

Anyway, there is not such thing as shifting by a negative number. When you shift an integer, only the lowest 5 bits are used. Hence, shifting by -2 is the same as shifting by 30. And shifting by -3 is the same as shifting by 29.

So...

Henry

Originally posted by Subramaniam Govindasamy:

Can any body help about shifting a integer left or right by a negative number.For example 4<<-2.

Here 4<<-2 gives 0.But 4<<-3 and so on gives non zero values(negative numbers also ).Justify this operation.

First of all...

Anyway, there is not such thing as shifting by a negative number. When you shift an integer, only the lowest 5 bits are used. Hence, shifting by -2 is the same as shifting by 30. And shifting by -3 is the same as shifting by 29.

So...

Henry

posted 12 years ago

...and when you shift by a long, only the lowest 6 bits are used.

Thus, not only is the shift quantity never negative, but it's always less than 32 for an int and less than 64 for a long.

Thus, not only is the shift quantity never negative, but it's always less than 32 for an int and less than 64 for a long.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org