Archived

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

archon

>> operator in C++

Recommended Posts

archon    122
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 this post


Link to post
Share on other sites
daerid    354
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 this post


Link to post
Share on other sites
Matt Calabrese    122
>> 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 this post


Link to post
Share on other sites
archon    122
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 this post


Link to post
Share on other sites
scaught    122
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.



Please, people, before trying to look smart and answer someone, know what you are talking about.

-8 >> 1 == -4

8 >> 1 == 4

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

-scott

Share this post


Link to post
Share on other sites
Matt Calabrese    122
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 this post


Link to post
Share on other sites
SilentCoder    122
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 this post


Link to post
Share on other sites
Matt Calabrese    122
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 this post


Link to post
Share on other sites
Beer Hunter    712
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 this post


Link to post
Share on other sites
Stoffel    250
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
Yeah, but don''t forget that it can''t optimize x / y, where y is a variable.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
In my opinion, it is a complete waste of time to write (x >> 1) when (x / 2) is clearer and more natural. The speed up would be virtually unmeasurable, and that''s only if the compiler is bad enough not to optimize it away.

In addition, the C++ standard makes no comment of how numbers are represented in binary format anyway, so this is making assumptions about what platform your using.

Share this post


Link to post
Share on other sites