Is there an operator in c to change the sign of int float etc. from negative to positive or vice versa?

trying to find the absolute value, and I thought that there is an easy way to just invert the character with "~" or something like that.

+7
operators objective-c absolute-value
source share
8 answers
float newValue = oldValue * -1; 

or

 float newValue = -(oldValue); //() aren't needed, I just use them out of habit 
+20
source share

A simple negation with - works, but most answers ignore the fact that the OP is trying to make an absolute value. The right tool for this is abs() for integers and fabs() for floats. The code will be crystal clear and the result will be what you expect. (Edit: be sure to read the documentation and errors for these tools. As Nick points out, negating the most negative number with abs() returns the same negative number.)

+12
source share

To invert a character, put a minus sign in front of it.

+10
source share

The unary negation operator -(expr) does exactly what you want.

 int x = -7; int y = 7; x = -x; // x is now 7 y = -y; // y is now -7 

The bitwise complement operator ~(expr) , which you mention, on the other hand, flips all the bits in the input.

In case this helps, one problem is that many implementations of the absolute value in ignoring wild ignore the fact that negating the most negative value of a given fixed size two-component type will overflow.

+8
source share

-x will give you the sign-inverted value of x.

+4
source share

Difficult subject. A direct way to change the sign to a floating point number is to flip the value of the most significant bit of the variable. The notations listed in the other answers are at the mercy of the compiler, which is usually good, but not always. For "float x" the answer is:

*(unsigned int*)&x ^= (1<<31)

If you're writing something like an iPhone that has a Cortex A8 processor (or something like that), you want to avoid duplication, and also avoid conventions when working with floats in internal loops. So you can do this:

*(unsigned int*)&x ^= ( (x<0) << 31 );

Which will turn negatives into positives without using a jump instruction. If it is in the inner loop, it will be 5-20 times faster than using another method on the iPhone. If this is not in the inner loop, then you probably care!

+3
source share

x = 0 - x;

? or am I missing a point?

+1
source share

It's a shitty decision, I don't know what happened here. See the answer below with abs () for the correct one.

Even if this is an old question, maybe this answer will help some of you. I wanted to have a positive or negative value for the number based on another result (say, the logical value mustBeNegative), as I did:

 int number = 7; if(mustBeNegative) { number = number * 1; } else { number = number * -1; } 

The number will be its positive or negative value based on the value of "mustBeNegative". When the number was already a negative number, it will become positive and vice versa.

-one
source share

All Articles