Log2 and Negatives

Hey everyone, need help with a simple program. Basically I need to use a ternary to compute the log2 of an integer that also protects against negative integer input AND converts the negative input to positive to output the log2 of that positive integer. If I'm making sense, here is my code:
1
2
3
4
5
6
7
8
9
10
11
{
	//compute logarithm to base 2
	float x;
	cout << "Enter an integer: ";
	cin >> x;
	double x1, x2;
	x1=log2(x);
	x2=log2(-x);
	cout << "The Logarithm to base 2 of your integer is: ";
	cout << (x>0 ? x1 : x2);
}


This code DOES work. It computes the log2 of a number fine, and if I put it in as a negative, it converts it and computes the proper log2 of it still. The problem is, if I understand my assignment correctly, is that I'm supposed to code without creating the variables x1 and x2, and have the entire conversion as part of the ternary. I tried that first, but I got an error that basically said I cannot have strings and numerical variables within a single ternary statement. Does anyone have any suggestions?
cout << log2(x < 0 ? -x : x) << '\n';
Thanks hayden, that's remarkably easy. I didn't even think of the brackets acting like parentheses of an algebraic expression, let alone having the log function only apply to the output variables. (I would have assumed it would log2 the all three of the x's, causing the expression to either always or never evaluate to true.) Thank you so much!
Topic archived. No new replies allowed.