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?

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

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

posted

0

thanks guys, Iv'e been practicing the concepts. It makes sence now.

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

posted

0

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 ]

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

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

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.