programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Shifting by negative values

Greenhorn
Posts: 1
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.

author
Sheriff
Posts: 23364
127

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

Sheriff
Posts: 11343
...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.

 These are not the droids you are looking for. Perhaps I can interest you in a tiny ad? The WEB SERVICES and JAX-RS Course https://coderanch.com/t/690789/WEB-SERVICES-JAX-RS