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 Shift operator Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Shift operator" Watch "Shift operator" New topic
Author

Shift operator

Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
int i = -3;
i >>> =1;
What's the value of i?
I understand that you have to convert -3 to binary and perform 2's compliment resulting in:
-3 = 1111 1111 1111 1111 1111 1111 1111 1101
You then perform the shift:
-2>>1= 1111 1111 1111 1111 1111 1111 1111 1110 =-2
What I don't understand is how the above bits result in -2. My understanding is for example, looking at the last 4 bits of information 1110 would result in 13.
Where am I going wrong with this?


Rob Petterson
SCJP
Doanh Nguyen
Ranch Hand

Joined: Dec 02, 2000
Posts: 45
Originally posted by Rob Petterson:
Where am I going wrong with this?

I think you mean i >>>=1; not i >>> =1;
>>> is an unsigned shift operator.
[ May 05, 2002: Message edited by: Doanh Nguyen ]
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Have you seen Cat and Mouse Games with Bits?

You're confusing >>> with >>

In the case of
-3 >>> 1 ;
the result is 2147483646.

In the case of
-3 >> 1 ;
the result is -2.

In the either case, as you said, you have to convert -3 to binary and perform 2's compliment resulting in:

-3 = 1111 1111 1111 1111 1111 1111 1111 1101

In the second case, you then perform the shift:
-3 >> 1 = 1111 1111 1111 1111 1111 1111 1111 1110 = -2

Whereas in the first case, you perform the shift:
-3 >>> 1 = 0111 1111 1111 1111 1111 1111 1111 1110 = 2147483646
[ May 05, 2002: Message edited by: Marilyn deQueiroz ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
prasad appecherla
Greenhorn

Joined: May 06, 2002
Posts: 7
After performing shift, you have:
-3 >> 1 = 1111 1111 1111 1111 1111 1111 1111 1110
-
We can see that the left most bit ( underlined ) bit is 1.The left most bit is "sign" bit and if the the value is "1" in that bit , it means that the binary is representing a "negative" value.
The value can be obtained by performing (inverse?) 2's complement on above binary and then making the number negative ( multiply by -1)
Step1:
perform (inverse?) 2's complementon above binary:
0000 0000 0000 0000 0000 0000 0000 0010 = 2
Step2:
Make the result negative:
2 * -1 = -2.

--------------------------------------------------
Originally posted by Rob Petterson:
int i = -3;
i >>> =1;
What's the value of i?
I understand that you have to convert -3 to binary and perform 2's compliment resulting in:
-3 = 1111 1111 1111 1111 1111 1111 1111 1101
You then perform the shift:
-2>>1= 1111 1111 1111 1111 1111 1111 1111 1110 =-2
What I don't understand is how the above bits result in -2. My understanding is for example, looking at the last 4 bits of information 1110 would result in 13.
Where am I going wrong with this?
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
thanks guys,
Iv'e been practicing the concepts. It makes sence now.
Rodney Woodruff
Ranch Hand

Joined: Dec 04, 2001
Posts: 80
Can somebody tell me why the following evaluates to -3? Thanks.
1111 1111 1111 1111 1111 1111 1111 1101
I have read the mouse and cat bit story and understand it quite well. However, I am little rusty on binary arithmatic.


Hope This Helps
Rob Petterson
Ranch Hand

Joined: Jan 23, 2002
Posts: 149
Hi Rodney,
I had the same issue as you until recently.
To get the result of -3 from this:
1111 1111 1111 1111 1111 1111 1111 1101
you have to minus 1 from low end bit(last digit on the right)
1111 1111 1111 1111 1111 1111 1111 1100
you then invert all bits
0000 0000 0000 0000 0000 0000 0000 0011
which results in 3
But remember we are dealing with a negative number here, so it is actually -3
If your'e not sure about binary addition/subtraction look at this site:
http://mathforum.org/dr.math/problems/houston.7.25.96.html
[ May 09, 2002: Message edited by: Rob Petterson ]
[ May 09, 2002: Message edited by: Rob Petterson ]
Steven Wong
Ranch Hand

Joined: Mar 07, 2002
Posts: 295
Hi,
1111 1111 1111 1111 1111 1111 1111 1101
You need to perform a 2's complement on it.
1. Invert all the bits, thus you will have:
0000 0000 0000 0000 0000 0000 0000 0010
2. Add a 1 to it.
0000 0000 0000 0000 0000 0000 0000 0011

which gives you 3.

3. And since it's a negative number (the most significant bit is 1) thus the result is -3.
Clement


best regards,<br />Steven<br />SCJP, SCEA
Manish Hatwalne
Ranch Hand

Joined: Sep 22, 2001
Posts: 2578

Originally posted by Rodney Woodruff:
Can somebody tell me why the following evaluates to -3? Thanks.
1111 1111 1111 1111 1111 1111 1111 1101
I have read the mouse and cat bit story and understand it quite well. However, I am little rusty on binary arithmatic.

Have a look at this and see if it is of any help.
HTH,
- Manish
 
 
subject: Shift operator
 
Similar Threads
A doubt in bitwise operator
Right Shift Operator Evaluation
using shift operators
i don't get shift operators!!!!
operators