This question already has answers here:
What is the "-->" operator in C++?
(29 answers)
Closed 1 year ago.
In various codes where time limit exceeded, by using while(t-- >0) instead of while(t--) code runs successfully. I don't know about while(t-- >0) , I read it somewhere in the codechef examples solution.
In practice, they are equivalent. It's just a matter of taste. Some think that while(t-- > 0) is clearer than while(t--).
As long as t is not negative, the two are completely equivalent. But on the other hand, a negative t would most likely indicate a bug with a loop like that.
Related
This question already has answers here:
Best practices for circular shift (rotate) operations in C++
(16 answers)
Closed 9 years ago.
I need to do a 3 bit left end around rotation in C++.
So far I have:
A[i] = (A[i] << 3)|(A[i] >> 5);
A is an unsigned char array.
Is this correct? If not how can I fix it? Also what is the best way to test and see if this is correct?
Thanks
Looks fine to me.
If you really want to test it, work out a bunch of inputs and outputs by hand and check your program produces them.
Or devise another method that you're absolutely sure will produce results (eg convert unsigned char to binary string, rotate the string, convert back to unsigned char) and compare the two against all 256 possible inputs.
This question already has answers here:
C: How to wrap a float to the interval [-pi, pi)
(15 answers)
Closed 9 years ago.
I'm wondering if it's possible to define a custom data type which can only take a value between -3.1415926535897932 and 3.1415926535897931.
The idea is that a value below or above the range would automatically "wrap-around", eliminating the need to write code to do the conversion and also eliminating the possibility of error somewhere.
Yes it is possible. In the method that sets the value check to see if it outside the limits and if so do whatever operation you want to do to force it to be inside the limits. fmod is one good choice for operation.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Using printf function
#include<iostream>
using namespace std;
int main()
{
long long a=20;
long long b=21;
printf("%d %d",a,b);
}
Output: 20 0
Can anyone please explain this behavior?
[EDIT]
I know %d is not the right way to print long long But main objective of posting this question is I want to know the behaviour of long long such that it is printing 0 for b while the correct value for a.
Pedantically speaking, your code invokes undefined behavior, because you've provided incorrect format specifier. You should use %lld instead of %d.
When it is undefined behavior, you cannot really reason out why it behaves like that. You may reason out for this input, but then that may fail for another set of input. Because it is undefined. Or you can see the documentation, it might say something about it, but it is not required to say anything why it is printing 0.
It looks like long long is a 64-bit type on your machine, while int is a 32-bit type. You must also be on a little-endian machine.
Because printf is a variadic function, the only way it can know what types you passed to it is by how you label the arguments in the format string. You are sending two 64-bit arguments, but only using two 32-but ones, according to your format string. That means the first print is the lower 32-bit "bottom half" of your 64-bit 20, and the second print is the 32-bit "top half" (which is, of course, 0). The 21 you passed is completely ignored.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C and C++ : Partial initialization of automatic structure
I'm looking for a fast way to init local array with zeros. (By "Fast", I mean "Fast to type.") When I do the following:
HANDLE hHandles[32] = {0};
does it zero out the first element or all 32?
It initializes all the 32 elements to zero.
See this surprisingly popular answer for details/alternatives. The difference between C and C++ seems to be that in C++ {} will do zero-initialization as well.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Is there a performance difference between i++ and ++i in C++?
I had an interview today and the interviewer asked me this question.
In C++ which is more "EFFICIENT" on non primitive types, A pre increment (++i) or a post increment (i++). I answered it by saying Pre increment but was not able to give the exact reason.
I have searched through the internet and found that Pre increment is more efficient. But I could not understand the reason. Can anyone please explain me the reason?
ex:
for ( ; c.value() != 21 ; ++c)
or
for ( ; c.value() != 21 ; c++)
pre-increment (++i) is usually faster since post-increment returns the current value and then increments the value, whereas pre-increment just increments the variable.
In many situations a compiler will optimise this anyway, unless you are using post-increment specifically to use the current value and increment the value afterwards.