views:

1616

answers:

6

I got this c++ macro and wonder what they mean by code%2 (the percentage sign) ?

#define SHUFFLE_STATEMENT_2(code, A, B)
switch (code%2)
{
  case 0 : A; B; break;
  case 1 : B; A; break;
}
+8  A: 

It means the remainder of a division. In your case, divide by 2 and the remainder will be either 0 or 1.

Mark Ransom
The remainder can be negative. The C++ standard only requires the equality (a/b)*b+(a%b)=a in case b!=0 and allows integer division to round towards zero (C++0x even requires it)
sellibitze
Although the remainder can only be negative if the dividend is negative (or the divisor, but that's 2 here). So depending what "code" is, that might not be an issue, and hopefully the programmer has thought of that when documenting how the SHUFFLE_STATEMENT_2 macro is to be used.
Steve Jessop
+5  A: 

It means modulo. Usually (x % 2) discriminates odd and even numbers.

mouviciel
+18  A: 

It is for taking a modulus.

Basically, it is an integer representation of the remainder.

So, if you divide by 2 you will have either 0 or 1 as a remainder.

This is a nice way to loop through numbers and if you want the even rows to be one color and the odd rows to be another, modulus 2 works well for an arbitrary number of rows.

James Black
+3  A: 

Thats the modulo. It returns whats left after division:

10/3 will give 3. - 1 is left.

10%3 gives this 1.

RED SOFT ADAIR
+2  A: 

Modulo returns the remainder that is left after division. It is helpful when you're tasked with determining even / odd / prime numbers as an example:

Here's an example of using it to find prime numbers:

int main(void)

{ int isPrime=1; int n;

cout << "Enter n: ";
cin >> n;

for (int i=1; i<=n; i++)
{
 for (int j=2; j <= sqrt(static_cast<double>(i)); j++)
 {
  if(!(i%j))
  {
   isPrime=0;
   break;
  }

 }

 if (isPrime)
  cout << i << " is prime" << endl;
 isPrime=1;
}
return 0;

}

JonH
+5  A: 

In case somebody happens to care: % really returns the remainder, NOT the modulus. As long as the numbers are positive, there's no difference.

For negative numbers there can be a difference though. For example, -3/2 can give two possible answers: -1 with a remainder of -1, or -2 with a remainder of 1. At least it's normally used in modular arithmetic, the modulus is always positive, so the first result does not correspond to a modulus. C and C++ allow either answer though, as long as / and % produce answers that work together so you can reproduce the input (i.e. -1x2+-1->-3, -2x2+1=-3).

Jerry Coffin
+1. In my opinion this is the only satisfying answer. The standard mentions the equlity (a/b)*b+(a%b)=a. Also, C++0x will add the guarantee that integer division always rounds towards zero. That makes -3/2=1 and -3%2=-1.
sellibitze
+1. And several times I have wanted to take the modulus of a negative number, but it's an instant guaranteed bug. It's a real flaw in C/C++: guaranteed to compile without a hitch, and might work on one platform, but instant (and mysterious) crash when compiled elsewhere.
Potatoswatter