• Advertisement
Sign in to follow this  

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.

If you intended to correct an error in the post then please contact us.

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

Share this post


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


Link to post
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 this post


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

Share this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
Also, what would you do if X was zero?

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

Share this post


Link to post
Share on other sites
Quote:
Original post by deEnumerator
Firstbetter 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 this post


Link to post
Share on other sites
Quote:
Original post by deEnumerator
no, 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 this post


Link to post
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 this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement