user2209008
user2209008

Reputation:

C++ modulus by 2 performance

I was wondering what the typical compiler's assembly reduction would be when performing an integer modulus by 2 operation such as this:

const char* integer_string = "300"; // avoid compiler optimization
int i = atoi(integer_string);
int b = i % 2; // the line in question

I'd imagine the compiler could turn it into a bit-wise operation to just check that last bit (1s place), but does it do this?

Upvotes: 1

Views: 123

Answers (1)

NPE
NPE

Reputation: 500337

The question only makes sense in the context of a particular compiler, platform, optimization options etc.

My compiler (gcc 4.7.2 on x86_64) does do this when -O3 optimizations are turned on:

    andl    $1, %esi

Upvotes: 7

Related Questions