#### Archived

This topic is now archived and is closed to further replies.

# >> operator in C++

This topic is 5962 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I am going through Bryan Turner''s RoamSimple.cpp class and I''ve come across this line: // Compute X coordinate of center of Hypotenuse int centerX = (leftX + rightX) >>1; I''m assuming >>1 somehow means divide by 2, but how? Feel free to be as condescending in your response as you''d like if this is something everyone should know. I can take it Wait! It still would be great if you responded.... Thanks for all the help,

##### Share on other sites
it''s called a bitwise shift operator.

it shifts all the bits to the right the specified number.

like this:

210 >> 1

Before:
11010010

After:
01101001

Effectively, it divides by 2^n where n is on the right of the >>

##### Share on other sites
>> is the right shift operator.

Since you are working in binary everything is in powers of 2 so a right shift is the same as dividing by 2 just like how if I were to shift 100 to the right once, I would get 100 divided by 10 which is 10 (because our numerical system is base 10 ).

However, there is a flaw in the example you provided. Because you are using the type int, and not unsigned int, the leftmost bit is used for sign. This means that if you had a negative value, then that 1 in the leftmost bit would be shifted and you wouldn't t get the number divided by two, you'd actually get the number divided by 2 plus 64.

EDIT: I'm always late!

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

[edited by - Matt Calabrese on March 19, 2002 7:14:49 PM]

##### Share on other sites
thanks for your help again, oh great fountain of knowledge.

one of these days I am going to create a loop of beautiful female voices singing in latin and play in while I''m programming to keep the feeling that angels are assisting me.

Thats a bit of an overstatement I guess, but I still want to make the loop.

##### Share on other sites
quote:
Original post by Matt Calabrese
However, there is a flaw in the example you provided. Because you are using the type int, and not unsigned int, the leftmost bit is used for sign. This means that if you had a negative value, then that 1 in the leftmost bit would be shifted and you wouldn''t t get the number divided by two, you''d actually get the number divided by 2 plus 64.

-8 >> 1 == -4

8 >> 1 == 4

>> works fine on negative numbers. (unless I''m special and it only works for me. )

-scott

##### Share on other sites
hmm, that''s weird! Is the right shift operator overloaded for signed datatypes to not shift the sign bit!? If it literally did right shit every bit, the outcome would be what I mentioned, but it must not.

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

##### Share on other sites
quote:
If it literally did right shit every bit, the outcome would be what I mentioned, but it must not.

Remember that signed values are stored in two's complement.

To represent a negative number, you take the absolute value of that number and invert all the bits, and then add 1 to it.

So, if you had -8 (00001000), you'd invert the bits to get 11110111. Then you'd add one to get 11111000. So, -8 in binary is 11111000, and 11111000 >> 1 would be 11111100 (-4).

[edited by - SilentCoder on March 19, 2002 8:14:07 PM]

##### Share on other sites
Thanks, I haven't really taken any classes so I've been learning C++ gradually over the last 4 months from just little bits and pieces of tutorials everywhere. I remembered reading that the first bit was the sign bit, but I didn't know the rest was inverted. Though that does make a lot of sense now that I think about it -- since it's all inverted that means that operations can be handled bitwise in the the same way wether negative or positive. Sorry for that! *crawls back into his shell*

--

On a side note, I just got an email response from the Yale College Composers' Group Composition Competition saying they recieved my song! Everyone head on over
Here and pick up a copy of my submittion! I'll know by the end of March if I won. Very general contest rules -- a song using one or any combination of 1 piano, 1 violin, 1 viola, 1 cello, 1 acoustic guitar, 1 vocal part.
Wish me luck -- this is also one of the songs I'm setting up for Realtime 3D Orchestra as a demo. Imagine watching a 3D piano with keys that go down corresponding to the notes that you can walk around and listen to in realtime. Should be done by the beginning of the summer.

Sorry for being off topic! I'll start another topic in "My announcements" for replies.

--------------------
Matthew Calabrese
Realtime 3D Orchestra:
Programmer, Composer,
and 3D Artist/Animator
"I can see the music..."

[edited by - Matt Calabrese on March 19, 2002 8:28:10 PM]

##### Share on other sites
The >> operator acts differently on signed and unsigned data types.

  #include <stdio.h>int main() {  union {    unsigned int unsigned_int;    signed int signed_int;  };  printf("unsigned shift:\n");  signed_int = -8;  unsigned_int >>= 1;  printf("%i\n", signed_int);  printf("signed shift:\n");  signed_int = -8;  signed_int >>= 1;  printf("%i\n", signed_int);}

##### Share on other sites
The behavior of padding ones when right-shifting signed types and zeros on unsigned types is known as "sign extension." Typical parlance is "the compiler will sign-extend the signed types when right-shifting." Just in case somebody ever asks you to describe sign extension in an interview.

Also, any compiler worth its salt (e.g. made in the last 10 years) will automatically convert divisions & multiplications by powers of two to the appropriate bitshift operations. There''s no need to obfuscate what''s being done--if you mean to divide by 32, don''t put >> 5.

1. 1
Rutin
19
2. 2
3. 3
JoeJ
16
4. 4
5. 5

• 26
• 20
• 13
• 13
• 17
• ### Forum Statistics

• Total Topics
631700
• Total Posts
3001781
×