Mail Archives: djgpp/1999/10/06/14:59:42
From: | vbresan AT jagor DOT srce DOT hr (Viktor Bresan)
|
Newsgroups: | comp.os.msdos.djgpp
|
Subject: | What's this? A bug?
|
Date: | 6 Oct 1999 17:10:51 +0200
|
Organization: | Sveucilisni Racunarski CEntar
|
Lines: | 50
|
Message-ID: | <MO8+3UJ05Q5a092yn@jagor.srce.hr>
|
NNTP-Posting-Host: | jagor.srce.hr
|
X-Trace: | bagan.srce.hr 939222682 20432 161.53.2.130 (6 Oct 1999 15:11:22 GMT)
|
X-Complaints-To: | news AT news DOT carnet DOT hr
|
NNTP-Posting-Date: | 6 Oct 1999 15:11:22 GMT
|
To: | djgpp AT delorie DOT com
|
DJ-Gateway: | from newsgroup comp.os.msdos.djgpp
|
Reply-To: | djgpp AT delorie DOT com
|
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 <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);
}
----------------------------------- cut - cut -------------------------------
--
Viktor Bresan, student of electronics at FESB Split, Croatia
http://public.srce.hr/~vbresan/
- Raw text -