Why am I getting a compiler warning for this if condition?
if(nowPlayingIndex-1 >= 0){ }
I am using this condition in a function and I am getting the following compiler warning in xCode:
开发者_运维百科Comparison of unsigned expression >= 0 is always true.
How can this be true? If the value of nowPlayingIndex
is <= 0
then the above condition is false.
Many thanks.
I'm sure the type of nowPlayingIndex
is unsigned integral type. If so, then nowPlayingIndex -1
will be unsigned integral type also, which can never be negative, hence nowPlayingIndex -1
is always greater than or equal to 0
.
Therefore, you should write :
if ( nowPlayingIndex >= 1 )
nowPlayingIndex
is apparently unsigned, so nowPlayingIndex-1
can never be negative. Therefore the condition is always true, as the compiler is warning you.
nowPlayinfIndex seems to be unsigned. This means it's always positive. If you go negative, you'll have a buffer overflow (underflow?)
Looking at how these things work in binary...Take an 8-bit signed integer for example:
10000000 (-127)
10000001 (-126)
with unsigned:
10000000 (127)
10000001 (128)
EDIT: Fixed the numbers
The left most bit, or the sign bit determines if your number is + or -. When it's 1, you can consider it to be -127, so when you add it to your running total, you get a negative number. However, with an 8-bit UNSIGNED integer, the sign bit has a value of +127. This is also why signed integers cannot store as large + numbers as unsigned.
If nowplayingindex
is unsigned, it cannot be < 0
. And if I subtract 1, unsignedness wins and I get the maximum value possible for this datatype, still being a positive number.
You must work with signed values or cast to such one.
You did not mention what is the datatype of nowPlayingIndex
see my comment.
If it is unsigned int
then that is your problem.
Just for fun, try to subtract 1 from an unsigned integer that is initilized with the value 0. Check your result.
That is a bit weird, but try the following and see if it helps.
if ((nowPlayingIndex - 1) >= 0)
{
//logic
}
Expanding on the earlier correct answers, if nowPlayingIndex == 0
, then nowPlayingIndex - 1
is the maximum value of its type, e.g., UINT_MAX, which might be 4294967295.
You probably want to write:
if (nowPlayingIndex > 0) { ... }
or
if (nowPlayingIndex >= 1 { ... }
if nowPlayingIndex is unsigned char
unsigned char nowPlayingIndex = 0;
nowPlayingIndex = nowPlayingIndex - 1; // 255 (UCHAR_MAX in limits.h)
It means exactly what it says on the tin. nowPlayingIndex is unsigned, nowPlayingIndex - 1 is unsigned as well, and all unsigned values are >= 0.
Try to use signed values instead. Ever since I rewrote all of my stuff to use signed values (my own int32 more precisely), everything is simpler and correctness is easier to ensure.
If that is too much effort, or not possible, simply use nowPlayingIndex >= 1.
The variable nowPlayingIndex is probably an unsigned integer, representing a positive number, it is always >= 0.
You are wrong, 0 - 1
can be greater than 0. I will show you:
$ cat foo.cpp
#include <iostream>
int main()
{
unsigned int foo = 0;
::std::cout << (foo - 1) << "\n";
return 0;
}
$ g++ -O3 foo.cpp -o foo
$ ./foo
4294967295
There you go.
精彩评论