0

I know the principle of a&1: for example:

4&1: 0100
     0001
   = 0000

and

5&1: 0101
     0001
   = 0001

But I want to know what and how is the principle of a%2

Which is faster, a%2 or a&1 ?

user3386109
  • 34,287
  • 7
  • 49
  • 68
  • 1
    This is implementation and setting dependent. Pick a compiler you care about, compile, and look at the machine code. – JohnFilleau Oct 10 '20 at 22:18
  • 4
    Shouldn't matter to any modern compiler. Whichever is better should be selected by the compiler and used no matter what you specify. – user4581301 Oct 10 '20 at 22:19
  • ok, now I'm lost. You wanna know the modulo operator? or you want to know which language is faster doing those operations? which? or... – Adrian Maire Oct 10 '20 at 22:21
  • You'll often find the result to be exactly equal. Test it here: https://quick-bench.com/ – Ted Lyngmo Oct 10 '20 at 22:26
  • Any compiler will choose the best way of doing it - even with no optimizations. Do now worry. https://godbolt.org/z/vPKzPj – 0___________ Oct 10 '20 at 22:28
  • @TedLyngmo always – 0___________ Oct 10 '20 at 22:32
  • i wanna know who is faster a%2 or a&1 i am not talking about any language – Fares Naoui Oct 10 '20 at 22:36
  • no simple answer - depends on the signes of the first operand. – 0___________ Oct 10 '20 at 22:37
  • @P__J__ I Said "_often_" to not step on any compiler implementors toes :-) There's no _rule_ that says that they must result in the same assembler code. – Ted Lyngmo Oct 10 '20 at 22:40
  • 1
    Although (a%2) will require a right-shift operation and a subtraction, modern processors perform instructions in parallel - it could still be faster than a single AND instruction. However unlikely, benchmarking is the only way to know for sure. – Sven Nilsson Oct 10 '20 at 22:47
  • duplicates: [What is the fastest way to find if a number is even or odd?](https://stackoverflow.com/q/2229107/995714), [which is faster to find the even number if(n%2==0) or if(n&1==0)?](https://stackoverflow.com/q/51597019/995714), [Are bitwise operators faster?, if yes then why?](https://stackoverflow.com/q/63096291/995714), [Why is %2 used rather than &1 to determine parity](https://stackoverflow.com/q/38906212/995714) – phuclv Oct 11 '20 at 01:11
  • @phuclv Not a duplicate: This is c/c++, that is python – pppery Oct 11 '20 at 01:42
  • @pppery I used the wrong link. I've already posted the C duplicates above: [What is the fastest way to find if a number is even or odd?](https://stackoverflow.com/q/2229107/995714) – phuclv Oct 11 '20 at 01:46
  • It depends on the compiler and, more importantly, on relative speed of different instructions on your target machine. Assuming `a` is an unsigned type, the result of `a %2` and `a&1` are identical, modern compilers (with sufficient code optimisation enabled) will detect that, and emit the same code for both. If `a` is a `signed` type, the result of the two expressions differ, so comparing their speed is pointless. – Peter Oct 11 '20 at 02:50
  • @user4581301 With modern you mean a compiler that is from the last 30 years or had significant improvements in the last 30 years? This is something almost every compiler can do. – 12431234123412341234123 Oct 12 '20 at 19:07
  • @12431234123412341234123 Thirty years sounds about right. I'd expect it to just happen debug build or no, but remember we still field a lot of questions on Turbo C++. But I promise nothing. I've had to use some pretty lame C compilers over the years. – user4581301 Oct 13 '20 at 00:44

1 Answers1

5

There are two cases:

  1. Unsigned integer. Any modern compiler will compile x % 2 as x & 1 even without the optimizations. So the speed will be the same as both will have the same machine code.
  2. Signed integer - the result of x % 2 and result of x & 1 are completely different and in this case you should use x % 2 to get modulo and x & 1 to test the LSB. https://godbolt.org/z/cEY4dM
user3386109
  • 34,287
  • 7
  • 49
  • 68
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    In the signed case, you should use `x&1` to get actual algebraic modulo or `x%2` to get the bogus C definition of modulo. :-) – R.. GitHub STOP HELPING ICE Oct 10 '20 at 23:53
  • 1
    It's not bogus, modulo is a signed operation. `-5 % 2 == -1`. – rustyx Oct 11 '20 at 00:17
  • 2
    @rusty This is getting OT, but certain operations depends on `x/3 - 1 == (x - 3) / 3` for all `x`. This is not the case with c-integer division which rounds toward zero rather than negative infinity. If integer division was rounding towards negative infinity, then `-5 / 3 == -2` and the corresponding modulo operator would yeld `-5 % 3 == 1` – HAL9000 Oct 11 '20 at 00:42
  • @rustyx The `%` is a reminder operation, which is not necessary the same as modulo. – 12431234123412341234123 Oct 12 '20 at 19:09