-
May 13th, 2011, 03:14 AM
#1
[RESOLVED] [newbie] What does 1 << VALUE do?
Hello
I need to understand what the following line does:
Code:
.end = 0x20000000 + (1 << MAX(VAL1,VAL2)))
What does the syntax "1 << SOMEVALUE" do?
Thank you.
-
May 13th, 2011, 03:16 AM
#2
Re: [newbie] What does 1 << VALUE do?
-
May 13th, 2011, 04:25 AM
#3
Re: [newbie] What does 1 << VALUE do?
So, with MYVALUE1=1 and MYVALUE2=2...
Code:
#defined MYCONST1 (1<<MYVALUE1)
#defined MYCONST2 (1<<MYVALUE2)
MYCONST1 now contains 2, and MYCONST2 contains 4.
Thanks for the link.
-
May 13th, 2011, 05:22 AM
#4
Re: [RESOLVED] [newbie] What does 1 << VALUE do?
Yes...
Although technically, there is no MYCONST1 or MYCONST2. What you did was define a macro named MYCONST1, which will be replaced by the text "(1<<MYVALUE1)" wherever it is encountered.
You should do this instead:
Code:
const int MYCONST1 (1<<MYVALUE1)
const int MYCONST2 (1<<MYVALUE2)
This has many advantages:
- Avoids errors due to collision: If someone else elsewhere has a "MYCONST1", you will receive a diagnostic
- Makes debugging easier: You'll have an actual symbol named MYCONST1.
- Tighter type safety: MYCONST1 has a pre-defined type. Any conversion that might lose information will give a diagnostic.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
May 13th, 2011, 11:40 AM
#5
Re: [RESOLVED] [newbie] What does 1 << VALUE do?
monarch_dodra was a bit quick there with the copy, paste & edit...
Code:
const int MYCONST1 = (1<<MYVALUE1);
const int MYCONST2 = (1<<MYVALUE2);
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|