modulus operation when one of the operand is negative value c++

TheGost

I have a vector<int> table and an int index=-833099133

When I write

cout<<table.size()<<endl;
cout<< index%table.size()<<endl;

It gives me :

83
  81

however If I write

cout<<index%83<<endl;

output turns out:

-79

Is there anyone to help me why it occurs ? thanks in advance

user529758

table.size() is of type std::vector<int>::size_type, which is an unsigned type (usually std::size_t), but the literal 83 is an int which is signed.

When performing an operation on a signed and an unsigned integer, the signed one is implicitly converted ("promoted") to an unsigned value. That results in a non-negative number which is the original value modulo some power of two (which power is used depends on the width of the unsigned type). In your case, size_t was 32 bits long, so

-833099133 == 3461868163 (mod 2 ^ 32)

and of course, 3461868163 % 83 is 81, whereas -833099133 % 83 is -79. (-833099133 mod 83 would be +4, but in C++, the % is not the modulo, but the remainder operator.)

Indeed, if you run the following program on a system where std::size_t is 32 bits long:

#include <iostream>

int main()
{
    int idx = -833099133;
    int signed83 = 83;
    std::size_t unsigned83 = 83;

    std::cout << idx % signed83 << std::endl;
    std::cout << idx % unsigned83 << std::endl;

    return 0;
}

you will get the same results.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ size_t modulus operation with negative operand

From Dev

C modulus returning negative number

From Dev

Why is the modulus operand not working on inputted variables in my C code?

From Dev

And bitwise operation gets negative value

From Dev

JavaScript (erroneously?) only applying modulus to operation with this one syntax

From Dev

Modulus Operation in Mitsubishi PLC

From Dev

Geometric Series Modulus operation

From Dev

Modulus between positive and negative number

From Dev

CMD set /a, modulus, and negative numbers

From Dev

Modulus operation with BigInteger returning 0

From Dev

Modulus Operation Returning Weird Results

From Dev

Select rows with at least one (any) negative value

From Dev

Select rows with at least one (any) negative value

From Dev

Invalid result when multiplying negative Duration value by a negative integer

From Dev

c++ getting a negative value when adding two absolute (positive) values

From Dev

Value Error: negative dimensions are not allowed when merging

From Java

C addition using modulus

From Dev

modulus optimization in c

From Dev

C Modulo operation differences with negative dividend and divisor is long unsigned

From Dev

Negative value in string based C# Calculator

From Dev

C struct timeval timersub() negative value to positive

From Dev

Why function is not returning negative value in C?

From Dev

lvalue required as left operand of assignment error when using C++

From Dev

decrementation and modulo - how to decrement negative value in one line of code

From Dev

c++ armadillo modulus function

From Dev

Number lines and modulus C++

From Dev

Why is the size of BitSet negative when using Integer.MAX_VALUE?

From Dev

Negative value when calculating the variance of an image -java-

From Dev

Big-O notation when the function has negative value

Related Related

  1. 1

    C++ size_t modulus operation with negative operand

  2. 2

    C modulus returning negative number

  3. 3

    Why is the modulus operand not working on inputted variables in my C code?

  4. 4

    And bitwise operation gets negative value

  5. 5

    JavaScript (erroneously?) only applying modulus to operation with this one syntax

  6. 6

    Modulus Operation in Mitsubishi PLC

  7. 7

    Geometric Series Modulus operation

  8. 8

    Modulus between positive and negative number

  9. 9

    CMD set /a, modulus, and negative numbers

  10. 10

    Modulus operation with BigInteger returning 0

  11. 11

    Modulus Operation Returning Weird Results

  12. 12

    Select rows with at least one (any) negative value

  13. 13

    Select rows with at least one (any) negative value

  14. 14

    Invalid result when multiplying negative Duration value by a negative integer

  15. 15

    c++ getting a negative value when adding two absolute (positive) values

  16. 16

    Value Error: negative dimensions are not allowed when merging

  17. 17

    C addition using modulus

  18. 18

    modulus optimization in c

  19. 19

    C Modulo operation differences with negative dividend and divisor is long unsigned

  20. 20

    Negative value in string based C# Calculator

  21. 21

    C struct timeval timersub() negative value to positive

  22. 22

    Why function is not returning negative value in C?

  23. 23

    lvalue required as left operand of assignment error when using C++

  24. 24

    decrementation and modulo - how to decrement negative value in one line of code

  25. 25

    c++ armadillo modulus function

  26. 26

    Number lines and modulus C++

  27. 27

    Why is the size of BitSet negative when using Integer.MAX_VALUE?

  28. 28

    Negative value when calculating the variance of an image -java-

  29. 29

    Big-O notation when the function has negative value

HotTag

Archive