473,659 Members | 3,494 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 2466
"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.un i-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.com base.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**********@n ews.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
1653
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 the base class of other containers. So why not use private instead of protected?
17
1926
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() { int i; int n = 20;
5
5274
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
1653
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
2207
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 little advanced for me, and I've also read that it focuses too much on encryption and security issues and doesn't really have coding problems, exactly. But something like that book would be fun to have.
6
1448
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 ......htdocsD -- the DOCUMENT_ROOT ...........index.html ...........topicsD ................topic1.html
3
1519
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 group_info.mess_list return linecount = 0
2
5164
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 using c#. 1 2 3 4 5 6 7 8
25
5448
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 looking out for language specific puzzles that can make me a top-notch JavaScript programmer. a) Any puzzles you can recommend? b) Any programs that you can suggest that can make me learn JavaScript internals in greatest depth. Please recommend...
62
12193
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 ones that my program cannot solve. However, most hard puzzles in the newspapers are solved within 2-3 cycles so far. The ones I've found on google which are said to be hard, get solved in 3 cycles. Any help would be very much appreciated!...
0
8428
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8337
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8628
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5650
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1978
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.