The % puzzles me.....

D

Don

I have found a little code piece saying:

int i,j;

void main(){

i=0; j=0;

i = (i+1) % 32;
j = (j+1) % 2;

}

what does this mean??? What do the % 32 and % 2 do?

Best Regards
Don
 
B

Ben Pfaff

Don said:
I have found a little code piece saying:

int i,j;

void main(){

main() always returns int in portable code.
i=0; j=0;

i = (i+1) % 32;
j = (j+1) % 2;

}

what does this mean??? What do the % 32 and % 2 do?

This is the "modulo" operator. It divides its first operand by
its second and yields the remainder. The code fragments
i = (i + 1) % 32;
and
i = i + 1;
if (i >= 32)
i = 0;
are equivalent, given that `i' is never assigned a value greater
than 31.

You should refer to your C reference manual for more details
(e.g. what happens when either operand is negative).
 
S

Steve Zimmerman

Don said:
> I have found a little code piece saying:
>
> int i,j;
>
> void main(){
>
> i=0; j=0;
>
> i = (i+1) % 32;
> j = (j+1) % 2;
>
> }
>
> what does this mean

Good question, Don.

It's simple. 5 % 2 means "the remainder of 5 divided by 2" (i.e., 1).

0 0
__ __
i = 32|1 j = 2|1
\ 0 \ 0
\ - \ -
--->1 -->1

The C standard--in its index--calls % the "remainder operator."

N869
6.5.5 Multiplicative operators
[#5]....The result of the % operator is the remainder [of
the division of the first operand by the second]....If the
value of the second operand is zero, the behavior is undefined.

Here's a program you can run to test the values yourself.

#include <stdio.h>

int main()
{
int i, j;

i = 0;
j = 0;

i = (i + 1) % 32;
j = (j + 1) % 2;

printf("i = %d, j = %d\n", i, j);

return 0;
}

--Steve
 
M

Micah Cowan

Don said:
I have found a little code piece saying:

int i,j;

void main(){

i=0; j=0;

i = (i+1) % 32;
j = (j+1) % 2;

}

what does this mean??? What do the % 32 and % 2 do?

Well, the "void main()" means undefined behavior. Don't do what you
see here--it's not conforming C code. main() always returns an int.

You are generally expected to have checked your C reference book
before posting here; any decent reference should explain the %
operator. But to answer your question, the expression

a % b

returns the remainder after dividing a by b. It will have the same
sign as a (i.e., -5 % 32 is -5).

-Micah
 
R

Rahul Agarkar

Who wrote this code ? Ask that person what did he/she intend to do
with this code ?

Anyway, % is the modulus operator in "c". This gives the remainder of
"a divided by b".

The c = a % b will return the remainder of the operation in the
variable "c".

Now, what this code is doing even i am not able to understand this...

Regards,
- Rahul Agarkar
 
O

olaf

Hello

The % means the modulo of a divide

5 % 2 = 1 => 5 / 2 = 2, 5 - (2*2) = 1
5 % 3 = 2 => 5 / 3 = 1, 5 - (3*1) = 2

it is bascily the rest of the integer devision.

Greetings Olaf
 
P

Peter Nilsson

Don said:
I have found a little code piece saying:

int i,j;

void main(){

int main() {
i=0; j=0;

i = (i+1) % 32;
j = (j+1) % 2;

}

what does this mean??? What do the % 32 and % 2 do?

By now you should know.

I just want to say that you should perhaps prefer an unsigned int over a
signed int for cycling between 0..(2**N-1). The reason is that some (many?)
compilers may not deduce that this is what the code is intended for and
thereby generate inefficient assembly code, rather than a simpler (and
generally much more efficient) bitmask.

Try disassembling the following functions and you might see what I mean.

int mod32(int x) { return x % 32; }

unsigned mod32u(unsigned x) { return x % 32; }

The first function is likely to be much more costly (slower) than the second
because the compiler must implement the semantics of the % operator, down to
correct results for a negative operand.

Whilst %32 is potentially more descriptive than &31, the results and
efficiency can be affected by the type which the operators are being applied
to.
 
A

Al Bowers

olaf said:
Hello

The % means the modulo of a divide

5 % 2 = 1 => 5 / 2 = 2, 5 - (2*2) = 1
5 % 3 = 2 => 5 / 3 = 1, 5 - (3*1) = 2

it is bascily the rest of the integer devision.

The standard uses:
(a/b)*b + a%b shall equal a.
where a and b are integer types and
b is not equal 0. (if the second operand, b, in a%b, is zero, you have
undefined behavior)
From this you can see that:
(3/2)*2 + 3%2 = 3
(3/2) equals 1
1*2 + (3%2) = 3
(3%2) = 3 - 2
(3%2) = 1

The integer types may be negative and the result may be
negative, even negative zero for implementations that support
a negative zero.
-3%2 = -1
-3%1 = 0 or (negative zero for implementations that support it).
 
V

Villy Kruse

Hello

The % means the modulo of a divide

5 % 2 = 1 => 5 / 2 = 2, 5 - (2*2) = 1
5 % 3 = 2 => 5 / 3 = 1, 5 - (3*1) = 2

it is bascily the rest of the integer devision.


There is a difference between modulo and reminder when negative numbers
gets involved. The reminder function is closely related to how you truncate
integer division results.


-5 % 3 = -2 => (-5) / 3 = (-1), (-5) - (3*(-1)) = (-5) - (-3) = (-2)
-4 % 3 = -1
-3 % 3 = 0
-2 % 3 = -2
-1 % 3 = -1
0 % 3 = 0
1 % 3 = 1
2 % 3 = 2
3 % 3 = 0
4 % 3 = 1
5 % 3 = 2

-5 modulo 3 = 1
-4 modulo 3 = 2
-3 modulo 3 = 0
-2 modulo 3 = 1
-1 modulo 3 = 2
0 modulo 3 = 0
1 modulo 3 = 1
2 modulo 3 = 2
3 modulo 3 = 0
4 modulo 3 = 1
5 modulo 3 = 2


Villy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top