CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2010
    Posts
    11

    [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.

  2. #2
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    Re: [newbie] What does 1 << VALUE do?


  3. #3
    Join Date
    May 2010
    Posts
    11

    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.

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  5. #5
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    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);
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured