C++ function for reversing signed/unsigned variable?
I was wondering if there was a C++ function to switch the sign of the variable to its opposite?
ie from -1 to 1
or from 1 to -1
What if the variable's value isn't known like below? Im working on the bottom 2 if's.
[source lang=cpp]void Ball::mBounceOffPaddle{ if (BALLy > 743 && BALLy < 760)//detect whether ball is in paddles Y region, if not-> dont test { if (BALLx >= PADDLEx - 57) || BALLx <= (PADDLEx + 57))//test for collision with paddle { if (BALLy <= (PADDLEy - 3) //test if collision took place on the side of the paddle { ballVelx = //change x velocity if the ball hit the side of the paddle } } else ballvely = }}
int ReverseSign (int const In)
{
return (-In) ;
//return (In * (-1)) ;
//return ((~In) + 1) ;
}
[smile]
{
return (-In) ;
//return (In * (-1)) ;
//return ((~In) + 1) ;
}
[smile]
First of all, signed and unsigned variables are different to variables that have the value 1 and -1.
With signed variables, it means that the most significant bit is used to keep track of whether the variable is positive or negative. If the variable is unsigned, is means that the variable value is always positive and can hold a larger range of positive values that the signed variables can.
If you're working with signed integers you can do something like this:
This performs 2's complement on the variable. I believe that this is how signed values are stored inside the computer. Anyway, try it out. It works.
Edit:: Why is it I always forget the simple things? "-a" <smacks self on head>
With signed variables, it means that the most significant bit is used to keep track of whether the variable is positive or negative. If the variable is unsigned, is means that the variable value is always positive and can hold a larger range of positive values that the signed variables can.
If you're working with signed integers you can do something like this:
int a = 23;a = ~a + 1; // boolean NOT operation on 'a' and add 1
This performs 2's complement on the variable. I believe that this is how signed values are stored inside the computer. Anyway, try it out. It works.
Edit:: Why is it I always forget the simple things? "-a" <smacks self on head>
Quote:Original post by deEnumerator
What if the variable's value isn't known like below? Im working on the bottom 2 if's.
*** Source Snippet Removed ***
Then Madhed's method would still work and be the best way to go about it.
well, seems you're doing some kind of arcanoid clone there. =)
Maybe you should rephrase your question... because to switch the sign
all you have to do really is i *= -1;
so if the ball hits the pedal in the center just do ballvely *= -1.
if it hits at the side do like above but change the x velocity too.
Maybe you should rephrase your question... because to switch the sign
all you have to do really is i *= -1;
so if the ball hits the pedal in the center just do ballvely *= -1.
if it hits at the side do like above but change the x velocity too.
Quote:Original post by jfclavette
Uh... guys.. what's with multiplying by -1 ?
i = -i;
If at any chance, the OP wanted to make the velocity larger or small, he can easily do i *= -1.1 (for a 10% increase) or i *= .9 (for a 10% decrease) [wink]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement