Why sizeof(int) is not greater than -1? [duplicate] - c++

This question already has answers here:
Comparison operation on unsigned and signed integers
(7 answers)
Closed 8 years ago.
this is my C code :
why is the output "False " ?????
why 4 > -1???
code :
#include <stdio.h>
int main() {
if (sizeof(int) > -1)
printf("True");
else
printf("False");
return 0;
}

Because sizeof(int) is unsigned. So -1 is converted to a large unsigned value.

Because sizeof yields a value of type size_t which is an unsigned type. In > expression usual arithmetic conversions will convert -1 to an unsigned type which is the type of the > result. The resulting value will be a huge positive value.
To get the expected behavior use:
(int) sizeof (int) > -1

Related

Why is the result value of the compare operation different from the mathematically same formula? [duplicate]

This question already has answers here:
c++ vector size. why -1 is greater than zero
(3 answers)
Closed 2 months ago.
I'm having trouble comparing size of a vector and simple constant -1
I believe both of these are logically the same:
(index >= (arr.size() - 1))
((index + 1) >= arr.size())
However, the first one returns 1 not 0.
What's the difference between the two comparisons?
#include <iostream>
#include <vector>
using namespace std;
int main() {
int index = -1;
vector<char> arr(6);
cout << (index >= (arr.size() - 1)) << endl;
cout << ((index + 1) >= arr.size()) << endl;
}
The arr.size method returns an unsigned integer type, so the type of the right-hand side of the comparison is unsigned. This results in the left side being converted to unsigned.
When the value on the left is -1, this gets converted to a very large unsigned number, resulting in the first comparison being true. In the second case, the value on the left is 0 so it doesn't change when being converted to an unsigned type and the comparison is false.
If you compile with -Wall -Wextra it will warn you about a signed / unsigned comparison.
size_t is an unsigned long long.
int is a signed type.
You are trying to compare an unsigned type with a signed one. The size of the vector will never have the value -1.

Why is leetcode modulo operator of negative numbers = 0? [duplicate]

This question already has answers here:
Why doesn't a negative number modulo a vector size give a negative number? [duplicate]
(5 answers)
Closed 1 year ago.
I was working on a practice coding question on leetcode in c++, and I found that using the modulo operator on negative numbers returned 0 when it should not be returning 0. For context, I was testing -4 % 3, which returns 0. Does anyone know why this happens?
Here is my code:
#include <iostream>
#include <vector>
int main()
{
std::vector<int> vec {1,2,3};
int k{4};
std::cout << (-k) % vec.size();
return 0;
}
This prints:
0
It is because in C++ when one operand of operator % is unsigned long long then other operand is also converted to it.
Unsigned number can not be negative.
So int -4 is implicitly converted to unsigned long long 18446744073709551612
that happens to divide by 3.

C++ : subtracting unsigned values is unsigned [duplicate]

This question already has answers here:
Is subtracting larger unsigned value from smaller in C++ undefined behaviour?
(2 answers)
Closed 3 years ago.
In C++ the compiler reminds me that subtracting unsigned values is unsigned and so calling abs() is pointless:
uint64_t a, b;
if (std::abs(a - b) > 10) {
std::cout << "Divergence achieved!" << std::endl;
}
OK, I understand that subtraction is addition, and I know in my instance that the numbers will be less than 2^63, so I static_cast to int64_t. But the point of calling abs was to avoid writing
if (a - b > 10 || b - a > 10) {
std::cout << "Divergence achieved!" << std::endl;
}
Is there a more idiomatic way to do this?
You can use std::minmax so you don't have to repeat the condition. This will give you a reference to the minimum and maximum values so you can always do the subtraction in the right direction. That would look like
uint64_t a, b;
auto minmax = std::minmax(a,b);
if (minmax.second - minmax.first > 10) {
std::cout << "Divergence achieved!" << std::endl;
}
Computers always subtract integers the same way - in a binary form. The assignment of signed or unsigned is an instruction to the compiler on how to treat the result.
Example: 1 - 2 (as 16 bit integers) will give 0xffff which is -1 for signed int, and 65535 for unsigned integer.
Therefore, if you want the signed difference between two unsigned integers, then
(int)(a - b);
I hope that helps.

Comparing int64_t and uint64_t [duplicate]

This question already has answers here:
Signed/unsigned comparisons
(6 answers)
Closed 7 years ago.
Does anybody know why this code produces such an output? -1 >= 0!!!
[mahmood#tiger ~]$ cat t.cpp
#include <iostream>
#include <stdint.h>
int main()
{
uint64_t i = 10;
uint64_t j = 10;
int64_t k = -1;
std::cout << "k=" << k << " i-j=" << i-j;
if (k >= i-j)
std::cout << " --> yes k>=i-j\n";
return 0;
}
[mahmood#tiger ~]$ g++ t.cpp
[mahmood#tiger ~]$ ./a.out
k=-1 i-j=0 --> yes k>=i-j
[mahmood#tiger ~]$
I know the types are different and a comparison need two similar types but at the end of the day, it is comparing -1 and 0. Isn't it?
if (k >= i-j)
both sides are converted to unsigned, so perhaps -1 is interpreted as 0xFFFFFFFFFFFFFFFF:
if (0xFFFFFFFFFFFFFFFF >= 0)
According to the standard (emphasis mine):
Expressions [expr]
Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.
No. It's likely comparing 0xffffffffffffffff to 0. The signed variable gets promoted to the unsigned type before the comparison is made. Look up 'arithmetic conversions' in the standard for more details.

A strange result occured in the following code(why input result 'b') [duplicate]

This question already has answers here:
void main() { if(sizeof(int) > -1) printf("true"); else printf("false"); ; [duplicate]
(3 answers)
Closed 8 years ago.
int main(void)
{
int array[] = {1,2,3,4,5,6,7};
int i = -1;
if(i <= (sizeof(array)/sizeof(array[0])) -2)
printf("a\n");
else
printf("b\n");
return(0);
}
I don't know why the IF expression is false.
You are trying to compare a signed integer int and an unsigned integer size_t.
Following C integer promotion rules, i gets promoted to an unsigned integer and therefore wraps to a very large number. When you compare that to the small value on the right side, the result is false.