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 11 1872
"Hur" <ja**********@y ahoo.com> wrote in message
news:ct******** **@delphi.et.tu delft.nl... 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;
}
dandelion wrote: "Hur" <ja**********@y ahoo.com> wrote in message news:ct******** **@delphi.et.tu delft.nl...
- 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.
"infobahn" <in******@btint ernet.com> wrote in message
news:42******** *******@btinter net.com... dandelion wrote: "Hur" <ja**********@y ahoo.com> wrote in message news:ct******** **@delphi.et.tu delft.nl...
- 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.
Shit, scheisse, merd'allors, kut met peren! Gretverdrie! ;-)
Absolutely right, of course. Never underestimate the problem. That should
indeed have been an unsigned int/long.
dandelion wrote: "Hur" <ja**********@y ahoo.com> wrote in message news:ct******** **@delphi.et.tu delft.nl...
<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
"Jonathan Burd" <jb***@nospam.c om> wrote in message
news:36******** *****@individua l.net...
<snip> Should be: if (0 != (value & 1))
cnt++; value >>= 1;
Sigh.... The dangers of writing ad hoc code.
Correct, of course.
On Wed, 2 Feb 2005 11:42:03 +0100, dandelion
<da*******@mead ow.net> wrote: "Hur" <ja**********@y ahoo.com> wrote in message news:ct******** **@delphi.et.tu delft.nl...
- 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(unsi gned long value)
{
int cnt = 0;
while (value)
{
++cnt;
value &= value - 1;
}
return cnt;
}
Although one could do it recursively:
int count_ones(unsi gned long value)
{
return 1 + count_ones(valu e & (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
Chris Croughton wrote: On Wed, 2 Feb 2005 11:42:03 +0100, dandelion <da*******@mead ow.net> wrote:
"Hur" <ja**********@y ahoo.com> wrote in message news:ct****** ****@delphi.et. tudelft.nl...
- 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_va lue
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(unsi gned long value) { int cnt = 0; while (value) { ++cnt; value &= value - 1; } return cnt; }
Although one could do it recursively:
int count_ones(unsi gned long value) { return 1 + count_ones(valu e & (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.
dandelion wrote: "Jonathan Burd" <jb***@nospam.c om> 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.c om, 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
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 >> 8) & 0x00ff00ff);
x = (x & 0x0000ffff) + ((x >> 16) & 0x0000ffff);
return x;
}
P.Krumins This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Elbert Lev |
last post by:
Please correct me if I'm wrong.
Python (as I understand) uses reference counting to determine when to
delete the object. As soon as the object goes out of the scope it is
deleted. Python does not use garbage collection (as Java does).
So if the script runs a loop:
for i in range(100):
f = Obj(i)
|
by: Justice |
last post by:
Currently I'm doing some experimenting with the XMLHTTP object in
Javascript. Now,
the XMLHttp object is asynchronous (at least in this case), and the
following code causes a significant memory loss even though I seem to be
allocaitng everything; help would be *vastly* appreciated. What am I doing
wrong here? I thought I was doing everything correctly (setting things to
null, for example) but none of the memory seems to get replaced. ...
|
by: tomvr |
last post by:
Hello
I have noticed some 'weird' memory usage in a vb.net windows app
The situation is as follows
I have an app (heavy on images) with 2 forms
(actually there are more forms and on starting the app I load some things into memory for global use of the app but I'll use only 2 starting forms to explain the situation)
situation 1
start app with form 1 (72mb memory usage), show form 2 and hide form 1 (89 mb memory usage
|
by: Tom |
last post by:
We have a VERY simple .NET C# Form Application, that has about a 23MB
Memory Footprint. It starts a window runs a process and does a regular
expression. I have done a GC.Collect to make sure that, no memory is
lying around. GC reports only 84k of allocations. Starting 5-10 of
this apps is going to start taking a considerable amount of memory. Is
there a way to reduce this?
Tom
|
by: Ian Taite |
last post by:
Hello,
I'm exploring why one of my C# .NET apps has "high" memory usage, and
whether I can reduce the memory usage.
I have an app that wakes up and processes text files into a database
periodically. What happens, is that the app reads the contents of a
text file line by line into an ArrayList. Each element of the ArrayList
is a string representing a record from the file. The ArrayList is then
processed, and the arraylist goes out of...
| |
by: Tony Johansson |
last post by:
Hello Experts!
I reading a book called programming with design pattern revealed
by Tomasz Muldner and here I read something that I don't understand
completely.
It says
"A garbarage collector, such as the one used in Java, maintains a record of
whether or not
an object is currentlys being used. An unused object is tagged as garbage,
|
by: lord trousers |
last post by:
I'm currently replacing the Quake 3 game code (not the rendering,
sound, or collision detection pieces) with Python. I've now
successfully loaded Python modules and made callbacks to them, rendered
maps, written some fly-through code, and embedded a Python interactive
mode into the console (which is way, way cool).
It's my first C API project, and I want to make sure I've got my
reference counting right. I *could* go through the API docs...
|
by: metdos |
last post by:
Is there a way calculating memory usage of program?
For example, I want to print to screen memory usage of my program in particular time periods.
Thanks.
|
by: Jean-Paul Calderone |
last post by:
On Tue, 22 Apr 2008 14:54:37 -0700 (PDT), yzghan@gmail.com wrote:
The test doesn't demonstrate any leaks. It does demonstrate that memory
usage can remain at or near peak memory usage even after the objects for
which that memory was allocated are no longer live in the process. This
is only a leak if peak memory goes up again each time you create any new
objects. Try repeated allocations of a large dictionary and observe how
memory...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| | |