Applying the modulo operation to an int value and unsigned integers

For example, the code below

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

Results:

-7
-7
-7

but if I changed type b to unsigned int, it has different meanings;

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

Reserves

9
9
-7

Can any body advise how it works here? How do differences arise?

Btw: I use C ++ in the latest version of Xcode.

+4
source share
1 answer

soybean <<a% b <ene;

a ( b unsigned) - . -7777 , 4294959519, 9 .

:

cout<< -7777%10 <<endl;

, int, -7.

+5

All Articles