Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Q, 1 counting and memory usage

Reply
Thread Tools

Q, 1 counting and memory usage

 
 
Hur
Guest
Posts: n/a
 
      02-02-2005
hi i ask two questions......someone can tell me

i an a linux gcc user and wanna know that

- how much physical memory is used for my c code ?

and another one is

- i need a (standard) function to count the number of '1'
for example,
1010 1000 0000 0000 ====> 3
0000 0000 1111 0000 ====> 4




 
Reply With Quote
 
 
 
 
dandelion
Guest
Posts: n/a
 
      02-02-2005

"Hur" <(E-Mail Removed)> wrote in message
news:ctqa43$vh4$(E-Mail Removed)...
> hi i ask two questions......someone can tell me
>
> i an a linux gcc user and wanna know that
>
> - how much physical memory is used for my c code ?


Depends on the program. Without you C-code, your compiler, the setting of
that compiler, it is simply impossible to tell. gcc -O3 will produce a
different number than gcc -Os, for instance, and the results will be
different depending for an i386 and an Atmel AVR.

> - i need a (standard) function to count the number of '1'
> for example,
> 1010 1000 0000 0000 ====> 3
> 0000 0000 1111 0000 ====> 4


That's not hard.

int count_ones(int value)
{
int cnt = 0;

while(value != 0)
{
if (0 != (cnt & 1))
cnt++;
value >>= 1;
}

return cnt;
}



 
Reply With Quote
 
 
 
 
infobahn
Guest
Posts: n/a
 
      02-02-2005
dandelion wrote:
>
> "Hur" <(E-Mail Removed)> wrote in message
> news:ctqa43$vh4$(E-Mail Removed)...
>
> > - i need a (standard) function to count the number of '1'
> > for example,
> > 1010 1000 0000 0000 ====> 3
> > 0000 0000 1111 0000 ====> 4

>
> That's not hard.
>
> int count_ones(int value)
> {
> int cnt = 0;
>
> while(value != 0)
> {
> if (0 != (cnt & 1))
> cnt++;
> value >>= 1;
> }
>
> return cnt;
> }


It's harder than it looks.

printf("%d\n", count_ones(-1));

On systems with sign bit propagation, this call could take some time.

Consider the merits of unsigned long.
 
Reply With Quote
 
dandelion
Guest
Posts: n/a
 
      02-02-2005

"infobahn" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> dandelion wrote:
> >
> > "Hur" <(E-Mail Removed)> wrote in message
> > news:ctqa43$vh4$(E-Mail Removed)...
> >
> > > - i need a (standard) function to count the number of '1'
> > > for example,
> > > 1010 1000 0000 0000 ====> 3
> > > 0000 0000 1111 0000 ====> 4

> >
> > That's not hard.
> >
> > int count_ones(int value)
> > {
> > int cnt = 0;
> >
> > while(value != 0)
> > {
> > if (0 != (cnt & 1))
> > cnt++;
> > value >>= 1;
> > }
> >
> > return cnt;
> > }

>
> It's harder than it looks.
>
> printf("%d\n", count_ones(-1));
>
> On systems with sign bit propagation, this call could take some time.
>
> Consider the merits of unsigned long.


****, scheisse, merd'allors, kut met peren! Gretverdrie!

Absolutely right, of course. Never underestimate the problem. That should
indeed have been an unsigned int/long.


 
Reply With Quote
 
Jonathan Burd
Guest
Posts: n/a
 
      02-02-2005
dandelion wrote:
> "Hur" <(E-Mail Removed)> wrote in message
> news:ctqa43$vh4$(E-Mail Removed)...
>


<snip>

>
> That's not hard.
>
> int count_ones(int value)


unsigned long value

> {
> int cnt = 0;
>
> while(value != 0)
> {
> if (0 != (cnt & 1))


Should be: if (0 != (value & 1))

> cnt++;
> value >>= 1;


<snip>

Regards,
Jonathan.


--
Email: "jonathan [period] burd [commercial-at] gmail [period] com" sans-WSP

"We must do something. This is something. Therefore, we must do this."
- Keith Thompson
 
Reply With Quote
 
dandelion
Guest
Posts: n/a
 
      02-02-2005

"Jonathan Burd" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
<snip>
> Should be: if (0 != (value & 1))
>
> > cnt++;
> > value >>= 1;


Sigh.... The dangers of writing ad hoc code.

Correct, of course.


 
Reply With Quote
 
Chris Croughton
Guest
Posts: n/a
 
      02-02-2005
On Wed, 2 Feb 2005 11:42:03 +0100, dandelion
<(E-Mail Removed)> wrote:

> "Hur" <(E-Mail Removed)> wrote in message
> news:ctqa43$vh4$(E-Mail Removed)...
>
>> - i need a (standard) function to count the number of '1'
>> for example,
>> 1010 1000 0000 0000 ====> 3
>> 0000 0000 1111 0000 ====> 4

>
> That's not hard.
>
> int count_ones(int value)
> {
> int cnt = 0;
>
> while(value != 0)
> {
> if (0 != (cnt & 1))
> cnt++;
> value >>= 1;
> }
>
> return cnt;
> }


Faster and more reliable (with negative inputs) is:

int count_ones(unsigned long value)
{
int cnt = 0;
while (value)
{
++cnt;
value &= value - 1;
}
return cnt;
}

Although one could do it recursively:

int count_ones(unsigned long value)
{
return 1 + count_ones(value & (value - 1));
}

Or in C99 #include <stdint.h> and use uintmax_t instead of unsigned
long, then the type will always be large enough.

Chris C
 
Reply With Quote
 
Michael Mair
Guest
Posts: n/a
 
      02-02-2005
Chris Croughton wrote:
> On Wed, 2 Feb 2005 11:42:03 +0100, dandelion
> <(E-Mail Removed)> wrote:
>
>
>>"Hur" <(E-Mail Removed)> wrote in message
>>news:ctqa43$vh4$(E-Mail Removed)...
>>
>>
>>>- i need a (standard) function to count the number of '1'
>>> for example,
>>> 1010 1000 0000 0000 ====> 3
>>> 0000 0000 1111 0000 ====> 4

>>
>>That's not hard.
>>
>>int count_ones(int value)
>>{
>> int cnt = 0;
>>
>> while(value != 0)
>> {
>> if (0 != (cnt & 1))
>> cnt++;
>> value >>= 1;
>> }
>>
>> return cnt;
>>}

>
>
> Faster and more reliable (with negative inputs) is:


The first is probably almost always true, the second not:
The representation of negative_value and 0UL+negative_value
may differ. If we have a logical right shift, the above
(with the corrections of the others) will work correctly.
I guess the most reliable (but not fastest) way is using
your code with unsigned char value and run through all
the bytes (which holds issues of its own; you essentially
can only wrap that up in a macro if you do not want to
prescribe a type, ...)


Cheers
Michael
>
> int count_ones(unsigned long value)
> {
> int cnt = 0;
> while (value)
> {
> ++cnt;
> value &= value - 1;
> }
> return cnt;
> }
>
> Although one could do it recursively:
>
> int count_ones(unsigned long value)
> {
> return 1 + count_ones(value & (value - 1));
> }
>
> Or in C99 #include <stdint.h> and use uintmax_t instead of unsigned
> long, then the type will always be large enough.
>
> Chris C

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      02-02-2005
dandelion wrote:
> "Jonathan Burd" <(E-Mail Removed)> wrote in message
>
> <snip>
>> Should be: if (0 != (value & 1))
>>
>>> cnt++;
>>> value >>= 1;

>
> Sigh.... The dangers of writing ad hoc code.
>
> Correct, of course.


Now, what is left to go wrong. Surely we can find another gaping
hole in the original code

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

 
Reply With Quote
 
Peteris Krumins
Guest
Posts: n/a
 
      02-03-2005
Hur wrote:
> hi i ask two questions......someone can tell me
>
> i an a linux gcc user and wanna know that
>
> - how much physical memory is used for my c code ?
>


As your C code gets compiled into assembler code and then
into machine code, and as the program gets loaded into memory,
the size of your c code == size of the machine code == size of .text
segment of your program + ((size of the machine code) mod (memory
alignment))

> and another one is
>
> - i need a (standard) function to count the number of '1'
> for example,
> 1010 1000 0000 0000 ====> 3
> 0000 0000 1111 0000 ====> 4


No such thing as standard way for counting number of 1 bits in byte.

Here is nice way:

int
cb(unsigned x)
{
x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
x = (x & 0x0f0f0f0f) + ((x >> 4) & 0x0f0f0f0f);
x = (x & 0x00ff00ff) + ((x >> & 0x00ff00ff);
x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff);
return x;
}


P.Krumins

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
What is the difference between Memory Usage and Heap Usage in my JVMMetrics ? Krist Java 8 02-10-2010 12:44 AM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:09 PM
retrieving CPU Usage and Memory Usage information in JAVA hvt Java 0 03-13-2007 01:07 PM
counting up instead of counting down edwardfredriks Javascript 6 09-07-2005 03:30 PM
Need help on memory usage VS PF usage metfan Java 2 10-21-2003 01:58 PM



Advertisments