简体   繁体   English

C ++模数乘以2的性能

[英]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: 我想知道当通过2这样的操作执行整数模数时,典型的编译器装配减少将是什么:

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? 我以为编译器可以将其转换为按位操作以仅检查最后一位(1s位),但是这样做吗?

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: 当打开-O3优化时,我的编译器( x86_64上的gcc 4.7.2 )会执行此操作:

    andl    $1, %esi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM