Public Group

# C++ function for reversing signed/unsigned variable?

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

## Recommended Posts

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

i *= -1;

or

i = i * -1;

;)

##### Share on other sites
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 = 	}}

##### Share on other sites
int ReverseSign (int const In)
{
return (-In) ;
//return (In * (-1)) ;
//return ((~In) + 1) ;
}

[smile]

##### Share on other sites
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:
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>

##### Share on other sites
there is also i = -i

##### Share on other sites
Quote:
 Original post by deEnumeratorWhat 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.

##### Share on other sites
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.

##### Share on other sites
Uh... guys.. what's with multiplying by -1 ?

i = -i;

##### Share on other sites
Quote:
 Original post by jfclavetteUh... 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]

• ### What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 9
• 9
• 9
• 34
• 16
• ### Forum Statistics

• Total Topics
634123
• Total Posts
3015654
×