Determine if subtracting from an unsigned int will roll over [duplicate] - c++

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?

Related

Weird behaviour when using the modulo operator between a negative int and std::size_t [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.
This code snippet:
#include <iostream>
#include <cstddef>
int main()
{
int a{-4};
std::size_t b{3};
std::cout << a % b;
return 0;
}
prints
0
while this code snippet:
#include <iostream>
int main()
{
int a{-4};
int b{3};
std::cout << a % b;
return 0;
}
prints
-1
So, why does the first code snippet returns 0? Why does changing b from std::size_t to int print the right result?
Oh, it's because a implicitly converted into std::size_t becuase b is std::size_t, which is 4294967292(on my machine), and 4294967292 % 3 == 0

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.

Endless loop when using unsigned int [duplicate]

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

(C++) Storing an int value of 10^80 [duplicate]

This question already has an answer here:
What variable type for extremely big integer numbers?
(1 answer)
Closed 3 years ago.
I have a problem where I need to work with variables
1 <= N <= 10^80
One of the test-cases works with a value:
3141592653589793238462643383279502884197169399375
and using unsigned long long int, the program sees this value maximum as:
18446744073709551615
Apparently, I need to store a value greater than that.
How do I solve this problem?
#include <iostream>
using namespace std;
int main()
{
unsigned long long int N;
cin >> N;
unsigned long long int Z;
int result = 0;
unsigned long long int num = N;
while (N > 0) {
Z += N % 10;
N /= 10;
}
while (Z % 9 != 0) {
Z += num;
result++;
}
cout << Z;
return 0;
}
In my opinion you should use big integer. There are few ways.
First, using C - Library, which has C++ interface. The GNU Multiple Precision Arithmetic Library: http://gmplib.org/
The second way - you should implement your own BigInteger Class.
template<class Type>
class BigInt
{
typedef typename Type BT;
protected:
std::vector<Type> value_;
};
In other words you just split the big number and record each part into the vector.

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.