ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I've only used that symbol for stream i/o. It looks like he declared an array of doubles but the 1<<12 I have never seen before.
What if someone says:
#define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
It looks to me like that replaces all
REP(x,y)
in your code with
for(x=0;x<(int)(y);x++)
But then what is this?:
REP(i,(1<<n)) REP(j,probs.size())
{
//blah blah blah
}
probs is a vector<int>
i and j are integers
n isn't anything. There is no n declared anywhere. I guess it has something to do with the #define above but I don't get it and I don't understand his use of '<<'.
There is no ^ for "to the power of" in C or C++. In fact ^ is bitwise XOR so perhaps my previous post wasn't completely robust.
As for the macro, I think that re-writing the for loop as a macro is pretty bad coding. In theory you could use the preprocessor to make C look like any nonsense language but it doesn't make it any more readable. The fact that you need to ask this is testament to that.
I wouldn't like to say for certain that your interpretation is correct but it looks right to me.
Was Vrajgh's answer enough?
x<<y is x shifted left y places, which assuming no overflow is (x times (2 to the y power))
Quote:
n isn't anything. There is no n declared anywhere.
If that is true then the code you found and quoted is wrong. The n in REP(i,(1<<n)) must refer to some n already declared at that time. It is safely distinct from the n in #define REP(i,n) for((i)=0;(i)<(int)(n);(i)++)
Quote:
Originally Posted by icecubeflower
Why would someone use 1<<n instead of 2^12? Is that common in the professional world?
Because x^y meaning x to the y power exists in other programming languages but not in C nor in C++.
1<<n has significant advantages over pow(2,n) so good professional programmers will use 1<<n rather than pow(2,n) and anyone programming in C or C++ should learn to recognize and understand that usage.
Oh yeah and I translated 1<<n to 4096 in my last example. Oops. What the hell is n? Hold on. (I wish I could copy and paste this guy's code but the stupid java applet won't let me.)
Hey you're right, there is an n. n is an int, passed to the function, sorry.
Yeah I understood the left shift thing. But what if you want to do a quick power of 3,5,6,7,9,etc... In that case do you have to use the pow() function?
I think the guy used all those annoying macros because it's a timed competition. I think he copies that garble into every competition. He has a ton of #include's that he didn't need either.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.