www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/10/07/02:16:56

From: "Damian Yerrick" <web DOT poison AT pineight DOT 8m DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: What's this? A bug?
Date: Wed, 6 Oct 1999 20:09:54 -0500
Organization: Rose-Hulman Institute of Technology
Lines: 69
Message-ID: <7tgs10$dhd$1@solomon.cs.rose-hulman.edu>
References: <MO8+3UJ05Q5a092yn AT jagor DOT srce DOT hr>
NNTP-Posting-Host: yerricde.laptop.rose-hulman.edu
X-Trace: solomon.cs.rose-hulman.edu 939258720 13869 137.112.205.146 (7 Oct 1999 01:12:00 GMT)
X-Complaints-To: news AT cs DOT rose-hulman DOT edu
NNTP-Posting-Date: 7 Oct 1999 01:12:00 GMT
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 5.00.2314.1300
X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2314.1300
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Viktor Bresan <vbresan AT jagor DOT srce DOT hr> wrote in message
news:MO8+3UJ05Q5a092yn AT jagor DOT srce DOT hr...
> 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)!?

"We all know" that int is implementation defined to match
the word size of the system.

> The same code compiled under the BC++ 3.1 acts normal
> (under 16 bits counted for each negative value).

The BC++ 3.1 implementation defines an int as 16 bits because
the 8086 processor defined an int as 16 bits, the size that
would fit into register AX. However, DJGPP is a 32-bit
compiler, and 32-bit compilers define int as 32 bits, the size
that fits into register EAX.

Try looking at the value of sizeof(int) in each of your compilers.

> Am i doing something wrong? What happends here?

You're just writing Borland C not portable C. If you want a 16-bit
integer, then use the type 'short'. I challenge c.o.m.d readers to
find *one* C/C++ compiler on *any* platform that defines 'short'
as anything other than a 16-bit integer. Change all instances of
'short' to 'int' and you'll be fine.

Oh, BTW, a cute symbol for 'cut' is
>8 >8 >8 >8 >8 >8  8< 8< 8< 8< 8< 8<

> #include <conio.h>
> #include <stdio.h>
>
>
> 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);
>  }



- Raw text -


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