Reputation: 73
int isNegative(int x) {
return ((unsigned) x)>> 31;
}
I'm writing a function that takes 32 bits and returns 1 if x<0, and 0 otherwise. How do I convert a signed int to an unsigned int without casting.
Upvotes: 1
Views: 2487
Reputation: 316
To have an unsigned version of an integer without casting, or creating a new number, multiply by -1 if negative (if a == INT_MAX, it is positive, and no multiplication will occur) :
#include <iostream>
int main (int argc, char* argv []) {
int a (-132);
std::cout << "a == " << a << std::endl;
if (a < 0) a *= -1;
std::cout << "unsigned a == " << a << std::endl;
return 0;
}
Upvotes: 0
Reputation: 180058
There is a variety of operations that perform automatic type conversions, among them assignments, arithmetic operations under certain conditions, and function calls (conversion of certain argument values).
Thus, you could achieve conversion of (signed
) int
arguments to unsigned int
simply by declaring that to be the parameter type:
int isNegative(unsigned x) {
return x >> 31;
}
Admittedly, however, that function interface could be a little confusing. You might therefore prefer to do this:
int isNegative(int x) {
unsigned ux = x;
return x >> 31;
}
However, I don't think either of those is as clear as your original version, with its cast. Type conversion is the entire purpose of a cast, and when conversion is what you need (and all that you need), a cast is the right tool for the job.
Of course, I do overall prefer the even simpler family of approaches suggested by @chux.
Upvotes: 1
Reputation: 153338
OP has different implied questions
a function that takes 32 bits and returns 1 if x<0, and 0 otherwise.
int isNegative(int x) {
return x < 0;
}
// or maybe return bool/_Bool
bool isNegative(int x) {
return x < 0;
}
// or pedantically
int isNegative(int_least32_t x) {
return x < 0;
}
// or pedantically and nearly universally portable,
int isNegative(int32_t x) {
return x < 0;
}
converting from signed int to unsigned int without casting (and)
How do I convert asigned int
to anunsigned int
without casting.
Simply assign the value
int i;
unsigned u = i;
Attempting to use >>
to combined these two risk implementation defined behavior and should be avoided unless compelling reasons are given.
EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bit when a signed integer is shifted right. C11§3.4.2 2
Upvotes: 4
Reputation: 34575
Your question is to write a function that takes 32 bits and returns 1
if x < 0
int isNegative(int x) {
return x < 0;
}
This will work no matter what the size of int
and does not invoke any type conversions. I suppose you have over-thought this problem.
Upvotes: 1
Reputation: 223689
In this case you don't need to. If you get rid of the cast, right shifting retains the sign bit.
Assuming int
is 32 bit, removing the cast results in -1 for negative numbers and 0 for nonnegative numbers. While the actual return value differs for the nonnegative case, in a Boolean context it will work as you expect.
Upvotes: 0