|
man ! do you look at your answers[^] when asking for something ?!
[edit]
ah I see, you deleted your post after my second reply to make people think I'm dumb ?
but you failed...
[/edit]
-- modified at 7:52 Tuesday 7th March, 2006
|
|
|
|
|
how to get the current system IP address?
JAYARAJ
|
|
|
|
|
|
i got it thanks
CString CIP::GetIP()
{
CString strIp;
//Init winsock
WSADATA wsaData;
int nErrorCode = WSAStartup(MAKEWORD(1,1), &wsaData);
if (nErrorCode != 0) {
//Cannot initialize winsock
return _T("");
}
char strHostName[81];
if (gethostname(strHostName, 80)==0)
{
hostent *pHost = gethostbyname(strHostName);
if (pHost->h_addrtype == AF_INET)
{
in_addr **ppip=(in_addr**)pHost->h_addr_list;
//Enumarate all addresses
while (*ppip)
{
in_addr ip=**ppip;
strIp = CString(inet_ntoa(ip));
ppip++;
if (strIp!=_T("")) {
break;
}
}
}
}
return strIp;
}
JAYARAJ
|
|
|
|
|
Hi,
Im using the CTime to calculate the date when adding number of days. The
strange thing is that when I add 58 and 59 days to the same date it returns
the same date. Here is the small code:
void DateCalc(int Days)
{
int Date=1;
int Month=9;
int Year1,Month1,Day1;
CTime Check( 2006, Month,Date,0,0,0);
Check+=CTimeSpan( Days,0,0,0);
Year1=Check.GetYear();
Month1=Check.GetMonth();
Day1=Check.GetDay();
TRACE("Year %d Month %d Date %d\n",Year1,Month1,Day1);
}
... function(...)
{
DateCalc(58); //add 58 days to 1/9/2006
DateCalc(59); //add 59 days to 1/9/2006
Both TRACE(..) functions within DateCalc(..) displays :
Year 2006 Month 10 Date 29
Year 2006 Month 10 Date 29
Anyone explain this?
Neil
|
|
|
|
|
Just try COleDateTime instead of CTime .
Nibu thomas
Software Developer
|
|
|
|
|
How do I add number of days to date? I tried the below, but didnt allow me to dd days to it.
int Month=9;
int Date=1;
COleDateTime Check( 2006, Month,Date,0,0,0);
Check+=CTimeSpan( 58,0,0,0);
Neil
|
|
|
|
|
NeilDevlin wrote: Check+=CTimeSpan( 58,0,0,0);
Check+=COleDateTimeSpan( 58,0,0,0);
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
That worked, thanks..
Neil
|
|
|
|
|
<wild guess>
British Summer Time ends on 29 Oct. My guess is that the CTime class is doing some BST/GMT stuff behind your back. I'd check the hour value of the Check variable, to see if that's changing.
</wild guess>
|
|
|
|
|
NeilDevlin wrote: Anyone explain this?
Go ahead and output the hour, minute, and second. Notice that the first one is 00:00:00 while the second one is 23:23:00. That BST thing is affecting you. To solve this, simply add another 0 to the CTime constructor.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
How do I find the number of bits set in a given integer number without using any while/for loop?
|
|
|
|
|
myInt mi = ;
int iCpt = 0;
for (int i = 0; i < sizeof(mi) * 8; i++) {
if ((mi & 0x1) == 0x1) {
iCpt++;
}
mi >> 1;
}
printf("mi contains %d '1'\n", iCpt);
ps: don't cross posts the forums. delete your question on the C++/CLI forum, as it is not a managed C++ question
-- modified at 6:51 Tuesday 7th March, 2006 (thanks stephen)
|
|
|
|
|
I don't believe this code will work:
v2.0 wrote: //For each bits in the integer
for (int i = 0; i < sizeof(mi); i++) {
Say "MyInt" is of type char . In this case sizeof(char) is 1. So the loop only loops once - But it should loop 8 times. With the following modification it will work:
for (int i = 0; i < sizeof(mi)*8; i++) {
PS: This assumes 8 bit chars - A fair assumption but not universally true on all platforms.
Steve
|
|
|
|
|
yep sorry, i fixed it consequently.
thanks
|
|
|
|
|
v2.0 wrote: for (int i = 0; i < sizeof(mi) * 8; i++) {
I believe that amar specifically indicated no while or for loops.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
|
|
|
|
|
yes, badKarma already notified me about that point that i did not see firstly
|
|
|
|
|
Given this:
#include <iostream>
#include <bitset>
using namespace std;
typedef bitset<sizeof(int)*8> IntBitSet;
You can count the bits like this:
cout << IntBitSet(42).count();
Steve
|
|
|
|
|
that's good, but very expensive way though
|
|
|
|
|
With templates only the methods actually called are compiled. Also, in general, the methods will be inline. That said the MSVC6 STL bitset class isn't as good as it could be and it will be slower then manually written code. With a good compiler (which MSVC6 is not) and a good STL there is no reason why it can't be just as efficient.
Steve
|
|
|
|
|
because it expands the bits to the array, and as the minimum unit that can be addressed is the byte (not the bit), then it might slower the treatment...
i'd recommend using bitarray when it's really needed, for huge bits operations for instance (refering to bjarne stroustrup reflection on the subject)...
|
|
|
|
|
v2.0 wrote: because it expands the bits to the array, and as the minimum unit that can be addressed is the byte
That is not how bitset s work. They store the data as bits and use bit operations ("and" & "or", etc) to address them. It has an embedded class called reference that makes this seamless but it does happen. Here's some code from MSVC6's implementation (I altered the formatting):
bitset<_N>& set(size_t _P, bool _X = true)
{
if (_N <= _P)
_Xran();
if (_X)
A[_P / _Nb] |= (_Ty)1 << _P % _Nb;
else
_A[_P / _Nb] &= ~((_Ty)1 << _P % _Nb);
return (*this);
}
This is the whole idea behind bitset s - To have the notational convince without space overheads you alluded to above.
Steve
|
|
|
|
|
and don't you think this is comsuming more cpu ?
firstly, you call a function, so unless it is inlined, there's all the call stack stuff that comes to work.
then each operation is actually a call to an operator function, and in a line like _A[_P / _Nb] &= ~((_Ty)1 << _P % _Nb) , you have about 7 operators called !!!
do you see what i want to show off ?
|
|
|
|
|
The functions are inline - functions which are defined and not just declared in the class definition are implicitly inline. The operators involved compile down to very simple machine code instructions, for example:
/32 : sar eax,5
%32 : and eax,0000001Fh
For a function which sets or clears a numbered bit there is no avoiding the &= , |= and << operators. As I said the MSVC6 implementation could be better:
- The constructors are not efficient.
- There is no specialization for the cases when the number of bits can fit into a machine word. If there was we could avoid the [] , / and % operations in these cases in the set method.
My main point however was to point out that the bitset doesn’t expand the data out to one byte per bit as you suggested. Also with a good implementation of STL and a decent compiler (I haven’t look under the hood of MCVC7’s STL) using a bitset is free (or close to).
Steve
|
|
|
|
|
If you realy worry about cpu cycles you should write in asm or at least in inline assembly
See my post here[^]
I agree that in most situation the bitset will prefrom more then adequate.
codito ergo sum
-- modified at 18:28 Tuesday 7th March, 2006
|
|
|
|