473,503 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

The % puzzles me.....

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
Nov 13 '05 #1
9 2459
"Don" <no**@spam.dk> writes:
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).
--
"I am worth much more brick-bat!!"
--Uday Joshi
Nov 13 '05 #2
Don wrote:
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

Nov 13 '05 #3
"Don" <no**@spam.dk> writes:
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
Nov 13 '05 #4
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

"Don" <no**@spam.dk> wrote in message news:<bk**********@news.net.uni-c.dk>...
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

Nov 13 '05 #5
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
Nov 13 '05 #6
"Don" <no**@spam.dk> wrote in message news:bk**********@news.net.uni-c.dk...
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.

--
Peter
Nov 13 '05 #7


olaf wrote:
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).

--
Al Bowers
Tampa, Fl USA
mailto: xa*@abowers.combase.com (remove the x)
http://www.geocities.com/abowers822/

Nov 13 '05 #8
On 22 Sep 2003 04:02:47 -0700,
olaf <o.*************@arcadis.nl> wrote:

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
Nov 13 '05 #9
In <bk**********@news.net.uni-c.dk> "Don" <no**@spam.dk> writes:
what does this mean??? What do the % 32 and % 2 do?


Ever considered reading a C book?

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1643
by: Kevin Wan | last post by:
Hi, I have some puzzles about sgistl source code, that is why some methods are placed in protected field. To vector itself, private and protected are the same, and vector isn't desired to be...
17
1904
by: madhav_a_kelkar | last post by:
i was browsing this problem: The following is a piece of C code, whose intention was to print a minus sign 20 times. But you can notice that, it doesn't work. #include <stdio.h> int main() {...
5
5253
by: PM | last post by:
Hello!!!! I am com sci student.... I am really interested in C... Does anybody know any free ebooks or websites for puzzles that test the intricacies of C... Please Help.... Casanova
2
1645
by: xPy | last post by:
do you know any links where i can find puzzles, like the 8 queens problem? (I'm not intrested in the solution, only for puzzle...)
11
2198
by: John Salerno | last post by:
Similar to the Python Challenge, does anyone know of any other websites or books that have programming puzzles to solve? I found a book called "Puzzles for Hackers", but it seems like it might be a...
6
1439
by: Steve Brecher | last post by:
Well, they are puzzles for me, anyway! On a linux/Apache shared host, a working web site has this directory structure (for clarity, each directory name ends with "D"): webD -- the FTP root...
3
1508
by: Sambo | last post by:
I have couple of puzzles in my code. def load_headers( group_info ): if os.path.isfile( group_info.pointer_file ): ptr_file = open( group_info.pointer_file, "r" ) else: print...
2
5156
by: =?Utf-8?B?Q3JtTmV3Ymll?= | last post by:
Hi, 1) I want to hone my problem solving skills and be good at logic. How do I achieve this? 2) Where can I find c# puzzles or c# algorithms. 3) How do I print the values of a N X N matrix...
25
5421
by: Oltmans | last post by:
Hi guys, I'm learning JavaScript and I need some puzzles that can make me a better JavaScript programmer. I mean I'm looking out for programming puzzles (e.g. Project Euler or TopCoder) but I'm...
62
12127
jkmyoung
by: jkmyoung | last post by:
Does anyone have some super, super hard Sudoku puzzles? Back in February this year, I had enough time to finally program a Sudoku solver in Java. Right now, I'm looking for solvable puzzles, but...
0
7091
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7282
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
741
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.