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

shift operators

Greenhorn
Posts: 29
How do shift operators work when the right operand is negative? for instance, how is the value calculated for
int result = 12 << -3;

Ranch Hand
Posts: 40
What I do is
12 << -3 <br /> take the last 5 bits (when LHS is int) --> 1 1101<br /> treat this as a normal +ve number<br /> 0001 1101 ---> 0x1D -- > 29
Now 12 is --> 0000 .....0000 1100
So its 000 ....0000 1100 << 29<br /> If you do this you'll find the most significant bit gets the 1<br /> --> 1000 ....0000 ... ---> -2147483648 or Integer.MIN_VALUE
This is the way I do and the answers come out right .I dont know whether its the correct method or not.
Can any one confirm if I am doing the right thing here..
Mousami