Applying modulo operation on a value of type int and unsigned integer

Viewed 1208

For example the code below

int a = -7777;
int b = 10;
cout<< a % b<<endl;
cout<<(a+b)%b<<endl;
cout<< -7777%10 <<endl;

The results are:

-7
-7
-7

but if I changed the type of b to unsigned int, it has different values;

int a = -7777;
unsigned int b = 10;
cout<< a % b<<endl;
cout<<(a+b)%b<<endl;
cout<< -7777%10 <<endl;

The resutls are

9
9
-7

Could any body advise how it is working here? How do the differences come?

Btw: I am using C++ in Xcode latest version.

1 Answers
Related