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.
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:
with unsigned:
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.