delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/04/17/17:01:36

From: "Elias Pschernig" <f DOT x DOT gruber-museum-arnsdorf AT magnet DOT at>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Bug in gcc 2.7.2?
Date: Fri, 17 Apr 1998 09:55:37 +0200
Organization: magnet Internet Services
Lines: 51
Message-ID: <6h8c4a$10a$1@orudios.magnet.at>
References: <35360BD4 DOT 5DB6C8C5 AT sis DOT co DOT at>
NNTP-Posting-Host: 195.3.67.133
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Gerhard W. Gruber schrieb in Nachricht <35360BD4 DOT 5DB6C8C5 AT sis DOT co DOT at>...
>I just encountered a strange thing. I was using a loop that process a
buffer.
>When the index is outside the buffersize the code should read the next
block
>from the file and continue. To do this I'm using the modulo operator but
there
>seems a bug with this.
>
>bl = BufLen % i;
>
>gives following results:
>BufLen = 4096
>i = 1
>bl = 0
>
thats correct

>i = 2
>bl = 0
>
>i = 3
>bl = 1 ???????

what did you expect ?
4096 div 3 = 1365 mod 1
(1365*3=4095 +1 =4096)

>
>All variables are unsigned long
>
>I just checked the code and substituted "bl = BufLen % i" with "bl = i /
BufLen"
>and this works fine.

" / " and " % " are different things.

" / " is the division operator
" % " is the modulo operator

In your case, to get the block number, you use " bl = i / Buflen "
and to get the position inside the block, you use " p = i % Buflen "



Elias Pschernig




- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019