• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Shift operator

 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Doanh Nguyen
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
prasad appecherla
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks guys,
Iv'e been practicing the concepts. It makes sence now.
 
Rodney Woodruff
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Petterson
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic