LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 11-24-2009, 09:41 AM   #1
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 304

Rep: Reputation: 33
syntax help


What does this mean?:
double dp[(1<<12)];

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 '<<'.
 
Old 11-24-2009, 09:45 AM   #2
Vrajgh
Member
 
Registered: Aug 2005
Posts: 61

Rep: Reputation: 20
<< is the binary "left shift" operator. 1<<12 is 2^12 or 4096
 
Old 11-24-2009, 10:19 AM   #3
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 304

Original Poster
Rep: Reputation: 33
So then
REP(i,(1<<n)) REP(j,probs.size())
means
Code:
for(i=0,i<(int)(4096);i++)
{
  for(j=0,j<(int)(probs.size()),j++)
  {
    //blah blah blah
  }
}
Right?

Why would someone use 1<<n instead of 2^12? Is that common in the professional world?
 
Old 11-24-2009, 10:27 AM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: washington U.S.
Distribution: Damn Small Linux, KateOs, M$ Ickdows Vista, My own OS
Posts: 2,094

Rep: Reputation: 104Reputation: 104
Quote:
Originally Posted by icecubeflower View Post
So then
REP(i,(1<<n)) REP(j,probs.size())
means
Code:
for(i=0,i<(int)(4096);i++)
{
  for(j=0,j<(int)(probs.size()),j++)
  {
    //blah blah blah
  }
}
Right?

Why would someone use 1<<n instead of 2^12? Is that common in the professional world?
very fast!
 
Old 11-24-2009, 10:31 AM   #5
Vrajgh
Member
 
Registered: Aug 2005
Posts: 61

Rep: Reputation: 20
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.

Last edited by Vrajgh; 11-24-2009 at 10:32 AM.
 
Old 11-24-2009, 10:32 AM   #6
johnsfine
Senior Member
 
Registered: Dec 2007
Distribution: Mepis, Centos
Posts: 4,005

Rep: Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731Reputation: 731
Quote:
Originally Posted by icecubeflower View Post
What does this mean?:
double dp[(1<<12)];
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 View Post
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.
 
Old 11-24-2009, 11:23 AM   #7
icecubeflower
Member
 
Registered: Mar 2008
Location: USA
Distribution: Slackware 13.1
Posts: 304

Original Poster
Rep: Reputation: 33
Oh right, there's no ^ operator for exponents.

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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[python] syntax Error : invalid syntax Python_user Programming 2 09-06-2009 12:52 PM
Starting httpd: httpd: Syntax error on line 209 of /etc/httpd/conf/httpd.conf: Syntax sethukpathi Linux - Networking 6 04-12-2008 11:26 AM
syntax SmithSmith123 Linux - Newbie 2 11-17-2005 04:45 PM
C++ syntax error before :: token HELP, i cant find the syntax error :( qwijibow Programming 2 12-14-2004 06:09 PM
syntax lynger Linux - Software 6 12-02-2003 01:03 AM


All times are GMT -5. The time now is 10:57 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration