WHY is comparison of unsigned expression < 0 always false - c++

So, if you ever try evaluating an unsigned integer < 0, you get a warning that says:
comparison of unsigned expression < 0 always false
This is because unsigned integers are never technically negative, here's the best explanation I've found so far: here
But if I run something like this:
for (int i = 0; i < 10; i++) {
testInt--;
NSLog(#"TestInt = %li", testInt);
// WARNING AREA //
if (testInt < 0) {
NSLog(#"HI");
}
// OK //
if (testInt == -1) {
NSLog(#"Negative ...");
}
}
I'll get output:
TestInt = 4
TestInt = 3
TestInt = 2
TestInt = 1
TestInt = 0
TestInt = -1
Negative ...
TestInt = -2
TestInt = -3
TestInt = -4
TestInt = -5
You can see that the number evaluates fine for negative values, and can equal -1.
Update
After #RobP's answer, I noticed a problem with my logging, it should be %lu which gives the output:
TestInt = 4
TestInt = 3
TestInt = 2
TestInt = 1
TestInt = 0
TestInt = 18446744073709551615
Negative ...
TestInt = 18446744073709551614
TestInt = 18446744073709551613
TestInt = 18446744073709551612
TestInt = 18446744073709551611
However, my question still stands because it can still evaulate numb == -1 and not numb < 0:
Question
Why can a NSUInteger be simultaneously equal to -1 but not less than 0?

The problem lies in your display of the numbers, not the numbers. This line:
NSLog(#"TestInt = %li", testInt);
takes your unsigned integer and displays it as if it's a signed int. Change the format specifier to %lu and you should see what your values "really" are to the machine.
EDIT:
Thanks to Massa for comment below about the equality of the comparison. When you compare your unsigned variable to the signed value -1, the -1 gets changed into an unsigned value (normally max value of an unsigned int) and they are equal, just as when you take an unsigned int equal to 0 and subtract 1, it becomes UINT_MAX.

There's no "technically" about it. Unsigned integers are never negative, full stop. They either hold 0 or a positive value. This doesn't need any further explanation.
They don't "remember" if they were originally assigned from a signed value, or anything like that.
As you discovered, your original code caused undefined behaviour by using the wrong format specifier for printf. All that tells you is (at best) that the bits used to store this unsigned int are the same bits that would be used to store a particular signed int. But the meaning of the bits is different, that's what data types do: they tell how a certain set of bits should be interpreted as a value.
In the code numb == -1 , there is a problem. You are using == on two different data types. In Java for example, the compiler rejects this outright. However, in C and C++ there are some rules called implicit conversions that will guess what you were most likely trying to do, and perform a conversion on one of the operands to match the other one.
In this particular case, the rule is that if one operand has type signed T and the other unsigned T, then the signed one is convered to unsigned. So your code actually behaves like:
numb == (unsigned long)-1
(assuming numb is unsigned long). Further, part of the definition of unsigned long is that when you assign to it an out-of-range value, that value is adjusted modulo ULONG_MAX + 1 until it comes in range. So the code is also equivalent to:
numb == ULONG_MAX
This is why the first output you see after 0 is ULONG_MAX.

Technically your number is never equal to -1. When you compare your unsigned number to a signed -1, the last is converted to an unsigned as well, hence yielding a value that is equal to testInt.
The comparison of an unsigned versus a 0 and mainly using the less operator is still always false, because 0 is an unsigned, hence no promotion from one data type to another doesn't takes place.

Related

why 0 < string.size() - N is true, while string.size() = 0

I am setting up a bool expression to entry loop; but the bool expression return a wrong answer?
s = "";
bool flag_1 = (0 < (s.size() - 10));
int temp = s.size() - 10;
bool flag_2 = 0 < temp;
//flag_2 and flag_1 should be the same,but they are different.
Assuming that s is a std::string, the std::string::size() method returns a std::string::size_type, which is an unsigned data type, and very often an unsigned long (which is 4 or 8 bytes, depending on platform).
If you subtract a positive value from an unsigned int containing 0, then the result will wrap to a very big positive number (ie UINT_MAX-10). Unsigned values are always positive, as its name suggests.
So, 0 compared < with such a wrapped number will be true.
If you assign that wrapped value to an int variable, the result will be negative (ie -10) . The compiler does an automatic integer conversion. Integer values can be negative.
That's the reason for the different results.

why declare "score[11] = {};" and "grade" as "unsigned" instead of "int'

I'm new to C++ and is trying to learn the concept of array. I saw this code snippet online. For the sample code below, does it make any difference to declare:
unsigned scores[11] = {};
unsigned grade;
as:
int scores[11] = {};
int grade;
I guess there must be a reason why score[11] = {}; and grade is declared as unsigned, but what is the reason behind it?
int main() {
unsigned scores[11] = {};
unsigned grade;
while (cin >> grade) {
if (0 <= grade <= 100) {
++scores[grade / 10];
}
}
for (int i = 0; i < 11; i++) {
cout << scores[i] << endl;
}
}
unsigned means that the variable will not hold a negative values (or even more accurate - It will not care about the sign-). It seems obvious that scores and grades are signless values (no one scores -25). So, it is natural to use unsigned.
But note that: if (0 <= grade <= 100) is redundant. if (grade <= 100) is enough since no negative values are allowed.
As Blastfurnace commented, if (0 <= grade <= 100) is not right even. if you want it like this you should write it as:
if (0 <= grade && grade <= 100)
Unsigned variables
Declaring a variable as unsigned int instead of int has 2 consequences:
It can't be negative. It provides you a guarantee that it never will be and therefore you don't need to check for it and handle special cases when writing code that only works with positive integers
As you have a limited size, it allows you to represent bigger numbers. On 32 bits, the biggest unsigned int is 4294967295 (2^32-1) whereas the biggest int is 2147483647 (2^31-1)
One consequence of using unsigned int is that arithmetic will be done in the set of unsigned int. So 9 - 10 = 4294967295 instead of -1 as no negative number can be encoded on unsigned int type. You will also have issues if you compare them to negative int.
More info on how negative integer are encoded.
Array initialization
For the array definition, if you just write:
unsigned int scores[11];
Then you have 11 uninitialized unsigned int that have potentially values different than 0.
If you write:
unsigned int scores[11] = {};
Then all int are initialized with their default value that is 0.
Note that if you write:
unsigned int scores[11] = { 1, 2 };
You will have the first int intialized to 1, the second to 2 and all the others to 0.
You can easily play a little bit with all these syntax to gain a better understanding of it.
Comparison
About the code:
if(0 <= grade <= 100)
as stated in the comments, this does not do what you expect. In fact, this will always evaluate to true and therefore execute the code in the if. Which means if you enter a grade of, say, 20000, you should have a core dump. The reason is that this:
0 <= grade <= 100
is equivalent to:
(0 <= grade) <= 100
And the first part is either true (implicitly converted to 1) or false (implicitly converted to 0). As both values are lower than 100, the second comparison is always true.
unsigned integers have some strange properties and you should avoid them unless you have a good reason. Gaining 1 extra bit of positive size, or expressing a constraint that a value may not be negative, are not good reasons.
unsigned integers implement arithmetic modulo UINT_MAX+1. By contrast, operations on signed integers represent the natural arithmetic that we are familiar with from school.
Overflow semantics
unsigned has well defined overflow; signed does not:
unsigned u = UINT_MAX;
u++; // u becomes 0
int i = INT_MAX;
i++; // undefined behaviour
This has the consequence that signed integer overflow can be caught during testing, while an unsigned overflow may silently do the wrong thing. So use unsigned only if you are sure you want to legalize overflow.
If you have a constraint that a value may not be negative, then you need a way to detect and reject negative values; int is perfect for this. An unsigned will accept a negative value and silently overflow it into a positive value.
Bit shift semantics
Bit shift of unsigned by an amount not greater than the number of bits in the data type is always well defined. Until C++20, bit shift of signed was undefined if it would cause a 1 in the sign bit to be shifted left, or implementation-defined if it would cause a 1 in the sign bit to be shifted right. Since C++20, signed right shift always preserves the sign, but signed left shift does not. So use unsigned for some kinds of bit twiddling operations.
Mixed sign operations
The built-in arithmetic operations always operate on operands of the same type. If they are supplied operands of different types, the "usual arithmetic conversions" coerce them into the same type, sometimes with surprising results:
unsigned u = 42;
std::cout << (u * -1); // 4294967254
std::cout << std::boolalpha << (u >= -1); // false
What's the difference?
Subtracting an unsigned from another unsigned yields an unsigned result, which means that the difference between 2 and 1 is 4294967295.
Double the max value
int uses one bit to represent the sign of the value. unsigned uses this bit as just another numerical bit. So typically, int has 31 numerical bits and unsigned has 32. This extra bit is often cited as a reason to use unsigned. But if 31 bits are insufficient for a particular purpose, then most likely 32 bits will also be insufficient, and you should be considering 64 bits or more.
Function overloading
The implicit conversion from int to unsigned has the same rank as the conversion from int to double, so the following example is ill formed:
void f(unsigned);
void f(double);
f(42); // error: ambiguous call to overloaded function
Interoperability
Many APIs (including the standard library) use unsigned types, often for misguided reasons. It is sensible to use unsigned to avoid mixed-sign operations when interacting with these APIs.
Appendix
The quoted snippet includes the expression 0 <= grade <= 100. This will first evaluate 0 <= grade, which is always true, because grade can't be negative. Then it will evaluate true <= 100, which is always true, because true is converted to the integer 1, and 1 <= 100 is true.
Yes it does make a difference. In the first case you declare an array of 11 elements a variable of type "unsigned int". In the second case you declare them as ints.
When the int is on 32 bits you can have values from the following ranges
–2,147,483,648 to 2,147,483,647 for plain int
0 to 4,294,967,295 for unsigned int
You normally declare something unsigned when you don't need negative numbers and you need that extra range given by unsigned. In your case I assume that that by declaring the variables unsigned, the developer doesn't accept negative scores and grades. You basically do a statistic of how many grades between 0 and 10 were introduced at the command line. So it looks like something to simulate a school grading system, therefore you don't have negative grades. But this is my opinion after reading the code.
Take a look at this post which explains what unsigned is:
what is the unsigned datatype?
As the name suggests, signed integers can be negative and unsigned cannot be. If we represent an integer with N bits then for unsigned the minimum value is 0 and the maximum value is 2^(N-1). If it is a signed integer of N bits then it can take the values from -2^(N-2) to 2^(N-2)-1. This is because we need 1-bit to represent the sign +/-
Ex: signed 3-bit integer (yes there are such things)
000 = 0
001 = 1
010 = 2
011 = 3
100 = -4
101 = -3
110 = -2
111 = -1
But, for unsigned it just represents the values [0,7]. The most significant bit (MSB) in the example signifies a negative value. That is, all values where the MSB is set are negative. Hence the apparent loss of a bit in its absolute values.
It also behaves as one might expect. If you increment -1 (111) we get (1 000) but since we don't have a fourth bit it simply "falls off the end" and we are left with 000.
The same applies to subtracting 1 from 0. First take the two's complement
111 = twos_complement(001)
and add it to 000 which yields 111 = -1 (from the table) which is what one might expect. What happens when you increment 011(=3) yielding 100(=-4) is perhaps not what one might expect and is at odds with our normal expectations. These overflows are troublesome with fixed point arithmetic and have to be dealt with.
One other thing worth pointing out is the a signed integer can take one negative value more than it can positive which has a consequence for rounding (when using integer to represent fixed point numbers for example) but am sure that's better covered in the DSP or signal processing forums.

Signed vs Unsigned comparison

#include <iostream>
int main()
{
signed int a = 5;
unsigned char b = -5;
unsigned int c = a > b;
std::cout << c << std::endl;
}
This code prints 0.
Can anyone please explain what is happening here? I am guessing that compiler converts a and b to same type(unsigend int maybe) and compares them.
Let's see how the computer stores the value b:
5 is 00000101, so -5 will be 11111011, so, when you convert it to unsigned char, it will became some positive number with value 11111011 in binary, which is larger than 00000101.
So, that's why a = 00000101 is smaller than b (0 means false).
It is printing 0 because a < b and 0 means false. The type of b is unsigned so it cannot hold negative numbers. Because of that -5 becomes 251 which is grater than 5.
Lets go to the third line in main
c take the value of 0 because a is not greater than b. This is because in C zero is considered false and everything then else is true.
With regards to b. Most platforms store negative integers using 2s complement format. So when we negate an number we flip all the bits and add 1. So -5 unsigned become 0xfa which is greater than 5.

Why does the unsigned int give a negative value in c++?

I have two functions add and main as follows.
int add(unsigned int a, unsigned int b)
{
return a+b;
}
int main()
{
unsigned int a,b;
cout << "Enter a value for a: ";
cin >> a;
cout << "Enter a value for b: ";
cin >> b;
cout << "a: " << a << " b: "<<b <<endl;
cout << "Result is: " << add(a,b) <<endl;
return 0;
}
When I run this program I get the following results:
Enter a value for a: -1
Enter a value for b: -2
a: 4294967295 b: 4294967294
Result is: -3
Why is the result -3?
Because add returns an int (no unsigned int) which cannot represent 4294967295 + 4294967294 = 4294967293 (unsigned integer arithmetic is defined mod 2^n with n = 32 in this case) because the result is too big.
Thus, you have signed integer overflow (or, more precisely, an implicit conversion from a source integer that cannot be represented as int) which has an implementation defined result, i.e. any output (that is representable as int) would be "correct".
The reason for getting exactly -3 is that the result is 2^32 - 3 and that gets converted to -3 on your system. But still, note that any result would be equally legal.
int add(unsigned int a, unsigned int b)
{
return a+b;
}
The expression a+b adds two operands of type unsigned int, yielding an unsigned int result. Unsigned addition, strictly speaking, does not "overflow"; rather than result is reduced modulo MAX + 1, where MAX is the maximum value of the unsigned type. In this case, assuming 32-bit unsigned int, the result of adding 4294967295 + 4294967294 is well defined: it's 4294967293, or 232-3.
Since add is defined to return an int result, the unsigned value is implicitly converted from unsigned int to int. Unlike an arithmetic overflow, an unsigned-to-signed conversion that can't be represented in the target type yields an implementation-defined result. On a typical implementation, such a conversion (where the source and target have the same size) will reinterpret the representation, yielding -3. Other results are possible, depending on the implementation, but not particularly likely.
As for why a and b were set to those values in the first place, apparently that's how cin >> a behaves when a is an unsigned value and the input is negative. I'm not sure whether that behavior is defined by the language, implementation-defined, or undefined. In any case, once you have those values, the result returned by add follows as described above.
If you are intending to return an unsigned int then you need to add unsigned to your function declaration. If you changed your return type to an unsigned int
and you use the values -1 & -2 then this will be your output:
a: 4294967295 b: 4294967294
Result: 4294967293
unsigned int ranges from [0, 4294967295] provided an unsigned int is 4bytes in size on your local machine. When you input -1 for an unsigned int you have buffer overflow and what happens here is the compiler will set -1 to be the largest possible valid number in an unsigned int. When you pass -2 into your function the same thing happens but you are being index back to the second largest value. With unsigned int there is no "sign" for negatives stored. If you take the largest possible value of an unsigned as stated above by (a) and add 1 it will give you a value of 0. These values are passed into the function, and the function creates 2 stack variables of local scope within this function.
So now you have a copy of a & b on the stack and a has a value of unsigned int max size and b has a value of unsigned int max size - 1. Now you are performing an addition on these two values which exceeds the max value size of an unsigned int and wrapping occurs in the opposite direction. So if the index value starts at .....95 and you add 1 this gives you 0 for the last digit, so if you take the second value which is max value - 1 : .....94 subtract one from it because we already reached 0 now we are in the positive direction. This will give you a result of ......93 which the function is returning if your return type is unsigned int.
This same concept applies if your return type is int, however what happens here
is this: The addition of the two unsigned values are the same result giving you
.....93 then the compiler will implicitly cast this to an int. Well here the range value for a signed int is -2147483648 to 2147483647 - one bit is used to store the (signed value) but it also depends on if two's compliment is being used etc.
The compiler here is smart enough to know that the signed int has a range of these values but the wrapping still occurs. What happens when we store 4294967293 into a singed int? Well the max value of int in the positive is 2147483647 so if we subtract the two (4294967293 - 2147483647) we would get 2147483646 left over. At this point does this value fit in the range of a signed int max value? It does however because of the implicit casting being done from an unsigned to a signed value we have 2 bits to account for, the signed itself and the wrapping value meaning max_value + 1 = 0 to account for, except this doesn't happen with signed values when you add 1 to max_value you get the largest possible -max_value.
For the unsigned values:
- ...95 = -1
- ...94 = -2
- ...93 = -3
With signed values the negative is preserved in its own bit, or if twos compliment is used etc., pending on the definitions to a signed int within the compiler. Here the compiler recognizes this unsigned value as being negative even though negative values are not stored in an unsigned for the sign is not preserved. So when it explicitly casts from an unsigned to signed one bit is used to store the signed value then the calculations are done and the wrapping occurs from a buffer overflow. So as we subtracted the actual unsigned value that would represent -3 : 4294967293 with the max+ value for a signed int 2147483647 we got the value 2147483646 now with signed ints if you add 1 to the max value it does not give you 0 like an unsigned does, it will give you the largest -signed value which for a 4byte int is -2147483648, since this does fit in the max+ value and the compiler knows that this is supposed to be a negative value if we add our remainder by the -max_value for a signed int : 2147483646 + (-2147483648) this will give us -2, but because of the fact that the wrapping with int is different we have to subtract 1 from this -2 - 1 = -3.
When it converts from an unsigned int to an int, the number is over what a signed integer can hold. This causes it to overflow as a negative value.
If you change the return value to an unsigned integer, your problem should be solved.

How the compiler knows whether to extend zeros or ones?

int main()
{
char c = -1;
unsigned char u = -1;
printf("c = %u\n",c);
printf("c = %d\n",c);
printf("u = %u\n",u);
printf("u = %d\n",u);
}
The result is:
c = 4294967295
c = -1
u = 255
u = 255
When I try to convert it into unsigned int
Because of sign extension I got c = 4294967295
But when I try to convert the unsigned char into unsigned int
I got u = 255
in the first case 1 extended by 16 bits and printed that number
in the second case 0 is extended.
But my question is how the compiler detects whether to extend zeros or ones when it is going to fit small data into a large memory.
Unsigned numbers (such as your u) will not be sign-extended, they will be zero-extended. That's because they have no sign! Signed numbers (i.e. variables of signed integral type) are always sign extended. The confusion may come from your initial assignment of -1 into an unsigned type--once it's assigned, there is no longer a possibility for sign extension.
The printing involves two steps.
a) The 8 bit chars (signed/unsigned) are converted to 32 bit
b) The 32 bit values is printed as signed or unsigned
So after step a) c=0b1111.1111.1111.1111.1111.1111.1111.1111 while u=0b0000.0000.0000.0000.0000.0000.1111.1111 due to the rule described by John Zwinck.