From: mrh99 AT uswest DOT net (Matthew Haley) Newsgroups: comp.os.msdos.djgpp Subject: Re: What's this? A bug? Organization: Home Message-ID: <37fb7201.1121707@news.uswest.net> References: X-Newsreader: Forte Free Agent 1.11/32.235 Lines: 66 Date: Wed, 06 Oct 1999 16:02:43 GMT NNTP-Posting-Host: 209.181.103.102 X-Trace: news.uswest.net 939225629 209.181.103.102 (Wed, 06 Oct 1999 11:00:29 CDT) NNTP-Posting-Date: Wed, 06 Oct 1999 11:00:29 CDT To: djgpp AT delorie DOT com DJ-Gateway: from newsgroup comp.os.msdos.djgpp Reply-To: djgpp AT delorie DOT com x-no-archive:yes Hi Viktor Bresan , An int is only guaranteed to be *at least* 16-bits. With a real compiler such as you have now, an int is 32-bits. On 6 Oct 1999 17:10:51 +0200, <> wrote: >Here is the code which counts the number of nonzero bits in int value. >It works fine until you enter negativ value, e.g. when i enter value -1 >it counts 32 nonzero bits (although we all know that int has only 16 bits)!? >The same code compiled under the BC++ 3.1 acts normal (under 16 bits counted >for each negative value). > >Am i doing something wrong? What happends here? >Thanx! > >--------------------------------- cut - cut --------------------------------- >#include >#include > > >char CntBits(int val) > { char ret_val; > int maska; > > ret_val = 0; > maska = 1; > while(maska) > { > if(val & maska) > ret_val++; > > maska <<= 1; > } > > return (ret_val); > } > >int main(/*int argc, char *argv[]*/) > { char CntBits(int); > int val; > > clrscr(); > printf("Enter int value: "); > scanf("%i",&val); > > printf("%i nonzero bit(s)!",CntBits(val)); > > return (0); > } > >----------------------------------- cut - cut ------------------------------- > > >-- >Viktor Bresan, student of electronics at FESB Split, Croatia >http://public.srce.hr/~vbresan/ =================================== Matthew Haley mrh99 AT uswest DOT net http://www.users.uswest.net/~mrh99 ===================================