|
I am new to C++ and was presented with this piece of code to try and figure out what it does. I was hoping that someone could tell me or give me some idea. Here is the code, for(c=a^b, cnt=0; c>0; cnt += c%2, c>>=1);
Thanks, Claytide
|
|
|
|
|
two variables are being set, c and cnt. I believe that ^ is exclusive or ( in other words, use only bits set in only one of the two numbers ), and % is definately remainder, so t will = 1 or 0, depending on if c is odd or even. I don't see how cnt t = c%2 works, is the second t a typo ? I presume so, and so if t = 1 and c is odd, or t = 0 and c is even, the loop ends.
>> is the shift operator, so if a right shift of the number results in a value of 1, the loop ends.
Christian
I have drunk the cool-aid and found it wan and bitter. - Chris Maunder
|
|
|
|
|
It's been a couple of years since I've done C++ but here goes:
a^b : ^ is a bitwise Exclusive-OR operator, e.g. 1^1=0; 1^2=3; 2^2=0; 32^64=96; 32^96=64;
Bitwise Exclusive OR Operator: ^[^]
c%2 : % is a modulus operator
C Multiplicative Operators[^]
>>= is a right-shift assignment operator
Shift Operators: >> and <<[^]
for
C++ language reference: The for statement[^]
c is initialised to a XOR b
cnt is initialised to zero.
The iteration will continue to loop around so long as c is greater than zero. This test is done at the start of each iteration.
At the end of each iteration cycle cnt is set to the remainder of c divided by 2 (In other words cnt will be 1 if c is odd, or zero if c is even) and c is shifted one bit to the right (in other words divided by two)
Now, while I was writing this, it occurred to me that this is an interview style question. No one in their right mind would code this.
So, for a bonus, (assuming c is an unsigned int and the code inside the loop doesn't alter c), what is the maximum number of iterations possible?
--Colin Mackay--
"In the confrontation between the stream and the rock, the stream always wins - not through strength but perseverance." (H. Jackson Brown)
|
|
|
|
|
It looks like you've mis-transcribed it a bit. That should be a 't', it should be a '+', giving:
for(c=a^b, cnt=0; c>0; cnt += c%2, c>>=1) ;
And that then clearly counts the number of bits that are different between 'a' and 'b'. *chuckles* Isn't that obvious?
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Is it possible to simulate mouse clicks?
|
|
|
|
|
Yes.
Just send (or post) WM_LBUTTONDBLCLK (and similar) messages to a target window. However, you need to deliver the messages to the right target window, which can be easier said than done. (Finding the window can be non-trivial).
/ravi
Let's put "civil" back in "civilization"
Home | Articles | Freeware | Music
ravib@ravib.com
|
|
|
|
|
How can I use a progress control to monitor the transfer status of file being transferred to the pocket pc device. Any ideas?
Thanks
|
|
|
|
|
Dont write the whole file with CeWriteFile, instead write it in parts so you can update your gui while writing.
Here's some pseudo-code doing that:
while (totalbyteswritten != totalsize)
{
CeWriteFile(hDest, buff+totalbyteswritten, min(chunksize, totalsize-totalbyteswritten), &byteswritten, 0))
totalbyteswritten += byteswritten;
}
“Our solar system is Jupiter and a bunch of junk” - Charley Lineweaver 2002
|
|
|
|
|
I want to convert int, long values to byte arrays, and from byte arrays back to int, long. I can do it with In .NET BitConverter class, but I want to do it in C++. any help greatly appreciated..
happy coding
|
|
|
|
|
Not sure if this is what you want:
char b[sizeof(int)];
int i=*reinterpret_cast<int *>(&b);
...
int i=...;
char b[4];
mempcy(b,&i,sizeof(int));
Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
|
|
|
|
|
Hi
I have a dialog which fits perfectly in a regular 17inch screen.
But with my friend, who has a 15inch screen, the dialog is too large to display.
How can i make sure the dialog got scrollbars on the 15inch screen, and not when the program is run with the 17inch screen?
Greetings
Bart
|
|
|
|
|
don't
Change your layout to fit the smaller screen size, using Tabs, and/or secondary dialogs.
Maximilien Lincourt
"Never underestimate the bandwidth of a station wagon filled with backup tapes." ("Computer Networks" by Andrew S Tannenbaum )
|
|
|
|
|
Oh, i've thought about that too
but that isn't possible.
|
|
|
|
|
Friends,
Please tell me that how can i change the background color of my dialog box at runtime.
Thanks.
|
|
|
|
|
Thanks for reply..one more question
How can i force repaint after applying your solution. For example on pressing the OK button of dialog box i call the following code:
AgxGetApp()->SetDialogBkColor( COLORREF clrCtlBk = RGB(192, 192, 192), COLORREF clrCtlText = RGB(0, 0, 0) );
But the problem is that the dialog is not repainted at that moment. Its only when i bring some other window on top of dialog, the dialog gets repainted with new background color. I also tried PostMessage(WM_PAINT) after the above line of code, but it is also not applying color ?
|
|
|
|
|
I want to list all sub-keys under a key. In my test I have two sub-keys. I open my key with ::RegOpenKeyEx() and then I put zero into first call for ::RegEnumKeyEx() . I call it again until it does not return ERROR_SUCCESS . This is my code:
HKEY hKey;
CHAR achKey[MAX_KEY_LENGTH];
DWORD i = 0;
if (::RegOpenKeyEx(HKEY_CURRENT_USER, REG_MAJOR_KEY, 0,
KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
{
while (::RegEnumKeyEx(hKey, i++, achKey, &cbName, NULL, NULL, NULL,
&ftLastWriteTime)) == ERROR_SUCCESS)
print("Found %s", achKey);
RegCloseKey(hKey);
}
The problem is it finds one key but not the other. Can anyone tell me where I am wrong please? Thank you.
|
|
|
|
|
Shouldn't you be checking against ERROR_NO_MORE_ITEMS instead?
while (::RegEnumKeyEx(hKey, i++, achKey, &cbName, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_NO_MORE_ITEMS)
...
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
|
Hello,
MFC CString has the += operator.. When dealing with char's how can one achieve the same thing?
I need to keep adding the contents of one char to the other while in a loop.
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
Use strcat() .
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
Thanks David!
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
One more question.. You answered my question correctly but I just realized that I was asking the wrong question.. Given the following code, can you give me any hints on how I can accomplish what I'm trying to do?
s_recv sRecv;
int nRecvSize = 0;
char outBuff[sizeof(s_recv)];
while(nRecvSize < sizeof(s_recv))
{
char inBuff[sizeof(s_recv)];
int nRecv = recv((SOCKET)wParam, inBuff, sizeof(inBuff), 0);
TRACE("Received %d of %d\n", nRecv, sizeof(s_recv));
if(nRecv == SOCKET_ERROR)
{
TRACE("MainFrm Recv Error %d\n", WSAGetLastError());
break;
}
else
{
if(nRecv == 0)
break;
strcat(outBuff, inBuff);
nRecvSize = nRecvSize + nRecv;
}
}
memcpy(&sRecv, outBuff, sizeof(outBuff));
Thanks for all the help...
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
RobJones wrote:
...can you give me any hints on how I can accomplish what I'm trying to do?
Assuming I knew what it is that you wanted to accomplish.
RobJones wrote:
strcat(outBuff, inBuff); // This doesn't work how I want it to..
So what is it doing instead?
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|
|
The structure is like this...
struct s_recv {
int nTag;
char sTagMsg[256];
char sMsg[3072];
};
When I do the receive sometimes I dont receive the whole structure just a peice of it.. So I'm trying to store what I have received in a char and then keep appending the data until all the data is received..
Here is a output of the strcpy...
The inBuff contains..
[0] -75 'µ'
[1] 6 ''
[2] 0 ''
[3] 0 ''
[4] 110 'n'
[5] 111 'o'
[6] 110 'n'
[7] 101 'e'
[8] 0 ''
[9] -51 'Í'
etc.......
This is fine.. But when I do the cat..
the outBuff looks like this...
[0] -75 'µ'
[1] 6 ''
[2] 0 ''
[3] 0 ''
[4] 0 ''
[5] 0 ''
[6] 0 ''
[7] 0 ''
[8] 0 ''
[9] 0 ''
etc......
I think I need some way of doing a memcpy that appends instead of strcat...
Rob
Whoever said nothing's impossible never tried slamming a revolving door!
|
|
|
|
|
RobJones wrote:
I think I need some way of doing a memcpy that appends instead of strcat...
You are correct. This is because the data you are receiving may have embedded '\0' characters, of which strcat() uses and memcpy() does not.
Try something like:
int nOffset = 0;
...
memcpy(outBuff + nOffset, inBuff, nRecv);
nOffset += nRecv;
Five birds are sitting on a fence.
Three of them decide to fly off.
How many are left?
|
|
|
|