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.
Related
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.
This question already has answers here:
Why does long long n = 2000*2000*2000*2000; overflow?
(6 answers)
C++ integer overflow
(4 answers)
Closed 1 year ago.
So my code looks like this:
unsigned long i=(27984 * 619246) + (1402 * 615589);
cout<<" i= "<<i;
I don't use the variable i anywhere else etc
Output looks like this
i= 1012166658
The correct answer is 18192035842 .
WHY is this happening?
Try this:
#include<iostream>
#include <string>
using namespace std;
int main() {
unsigned long long i = (27984ULL * 619246ULL) + (1402ULL * 615589ULL);
cout << " i= " << i;
}
using the long long literal suffix LL, and upgrading the type to a bigger type solves the problem.
I use the U (unsigned) literal suffix so that we can have it the same as the resulting type.
With your original code, there was an overflow (the result did not fit in the original type, or the operator) so it returned a truncated version. An example compiler warning that I got:
warning C4307: '*': signed integral constant overflow
This question already has answers here:
Infinite loop when using size_t in a count down for loop
(6 answers)
Closed 2 years ago.
I have written a function that has to return n of bit "1" in binary representation. I have observed unexpected (for me) behavior.
When I'm using unsigned int type in a for loop, the function gets stuck in an endless loop, I have no idea why.
unsigned int countBits(unsigned long long n)
{
//your code here
int nofbits= log2(n)+1,
nofone=0;
for(int i=nofbits;i>=0;--i)
{
if(n-pow(2,i)>=0)
{
nofone++;
n=n-pow(2,i);
}
}
return nofone;
}
vs with endless loop
unsigned int countBits(unsigned long long n)
{
//your code here
int nofbits= log2(n)+1,
nofone=0;
for(unsigned i=nofbits;i>=0;--i)
{
if(n-pow(2,i)>=0)
{
nofone++;
n=n-pow(2,i);
}
}
return nofone;
}
As the name of the type indicates, an unsigned int can never become negative. Decrementing an unsigned int value 0 results in the maximum value an unsigned int can take on. Hence, a comparison like i >= 0 with i being of type unsigned int will always be true.
for(unsigned i=nofbits;i>=0;--i)
it will always be greater than 0 because it is unsigned. An unsigned number can only be >= 0.
From the comments, Remy Lebeau commented that when you subtract 1 from an unsigned 0, it will wrap around you end up with a very large number: 4294967295
This question already has answers here:
Checking for underflow/overflow in C++?
(5 answers)
Saturating subtract/add for unsigned bytes
(11 answers)
Closed 3 years ago.
How can it be possible to determine if subtracting n from an unsigned int will roll over to a negative value, considering that casting to a signed int can result in a negative value already?
Example
#include <iostream>
using namespace std;
int main(){
unsigned int i = 2147483647*2;
if((int)i - 1 < 0){
cout << "rolled over";
}
else {
i = 0;
}
return 0;
}
In order to check if subtracting from an unsigned int would roll over, you could cast it to an int first. However, if the unsigned int is > the max int value, you will already end up negative. So how can I prevent an unsigned int from rolling over?
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