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

This topic is 4431 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]

##### Share on other sites
First, thanks for the responses :D

Hmm, what im trying to do is change the ball's Y direction if it hits the paddle or change the ball's X direction if it only hits the side of the paddle. Its Pong. In other words, I was just curious if there is a C++ function that would take a variable and automatically reverse its value.

better example:(?)

int reverseValue(X)
{
if (X < 0) return abs(X);
if (X > 0) return -X;
}

(I think thats right)

@Skeleton, I'll have to read up on that one. Thanks

##### Share on other sites
You're not going to use that function are you?

Just use " i = -i; ". I mean, just think of it just multiplying by -1. What's -1 * -1 ? It's 1.

##### Share on other sites
Also, what would you do if X was zero?

int negateNumber(int the_number){	return -the_number;}

##### Share on other sites
Quote:
 Original post by deEnumeratorFirstbetter example:(?)int reverseValue(X){if (X < 0) return abs(X);if (X > 0) return -X;}

As others have already stated, you don't need a function. The line
    x = -x;
will do just want you want. If you insist on a function, it might look like this:
    int reverseValue( int x )    {        return -x;    }    ...    i = reverseValue( i );
or perhaps this
void reverseValue( int & x )    {        x = -x;    }    ...    reverseValue( i );

##### Share on other sites
no, i'd use the 1 *= -1, so theres not a function like i the horrible example i gave?

##### Share on other sites
Quote:
 Original post by deEnumeratorno, i'd use the 1 *= -1, so theres not a function like i the horrible example i gave?

Actually, there is a function! The name of the function is operator-. You usually call it by using the '-' operator, but you could call it like this:
    x = operator-( x );

##### Share on other sites
Sorry guys, im a newbie, it just seems reasonable to me given all the other handy functions C++ has that they would have included a similar one to the above.

@_goat: good question.

##### Share on other sites
I GET IT!

if x is a negative number, negative * negative....omg i should just quit while i'm ...behind.