File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes shifting problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "shifting problem" Watch "shifting problem" New topic
Author

shifting problem

payal sharma
Ranch Hand

Joined: Oct 04, 2000
Posts: 57
given
i=-1
-1>>31;
what I have done after reading a post here
first of written one as
00000000 00000000 00000000 00000000 00000001
then I inverted all bits
11111111 11111111 11111111 11111110
then I add one to it
11111111 11111111 11111111 11111111
then when i shifted it to right 31 bit
00000000 00000000 00000000 00000001
now the value is 1
Answer is -1 which is correct
where I made the mistake?

Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Payal,
This is the way you have to go about byte shifting
<pre>
1 ===> 0000 0000 0000 0000 0000 0000 0000 0001
~(inverse) ===> 1111 1111 1111 1111 1111 1111 1111 1110
Add 1 ===> 1
-1 ===> 1111 1111 1111 1111 1111 1111 1111 1111
>> 31 bits ===> 1111 1111 1111 1111 1111 1111 1111 1111
</pre>

Note that, while doing a right shift you have to take fill the places with the sign bit in the left.In this case it is 1 and not 0.
So the result is -1 and not 1.
Hope this helps,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)


<b>Sandeep</b> <br /> <br /><b>Sun Certified Programmer for Java 2 Platform</b><br /> <br /><b>Oracle Certified Solution Developer - JDeveloper</b><br /><b>-- Oracle JDeveloper Rel. 3.0 - Develop Database Applications with Java </b><br /><b>-- Object-Oriented Analysis and Design with UML</b><br /> <br /><b>Oracle Certified Enterprise Developer - Oracle Internet Platform</b><br /><b>-- Enterprise Connectivity with J2EE </b><br /><b>-- Enterprise Development on the Oracle Internet Platform </b>
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Rajani,
-1 is not represented as
1000 0000 0000 0000 0000 0000 0000 0001
in binary format!
-- Sandeep
Mini Pilla
Ranch Hand

Joined: Jul 15, 2001
Posts: 112
I am sorry ,I used wrong words!!! Of course that is not a binary represetation but I thought that will help to solve the questions in the exam point of view!!!
Thanx
Rajani

Originally posted by Desai Sandeep:
Rajani,
-1 is not represented as
1000 0000 0000 0000 0000 0000 0000 0001
in binary format!
-- Sandeep

Ashish Hareet
Ranch Hand

Joined: Jul 14, 2001
Posts: 375
Hi Payal ,
Just my addition
When you are shifthing right ( >> ) --- the leftmost(top) bits exposed by the right shift are filled in with the previous contents of the top bit & not just zero's .
We would've got the answer as 1 if we were using " >>> " instead of " >> " .
" >>> " always shifts zero's into the high order bit .
This should help you explain the answer .
The interesting thing about your question is that no matter how much you use " >> " to shift -1 to the right the result would always be -1 .
Hope hat helps
[This message has been edited by Ashish Hareet (edited July 27, 2001).]
Bill Tripper
Greenhorn

Joined: May 30, 2001
Posts: 24
Originally posted by payal sharma:
given
i=-1
-1>>31;

Am I missing something here? The way I figure this is:
i=-1
-1>>31;
is the same as
i=-1 -1>>31;
or
i = (-1) - (1>>31);
or
i = (-1) - 0
or
i = -1
Bill

Jon Miller
Greenhorn

Joined: Jul 27, 2001
Posts: 7
From the code originally submitted I would have to agree with the last posting!
 
jQuery in Action, 2nd edition
 
subject: shifting problem