Leftshift???

This is a discussion on Leftshift??? within the C Programming forums, part of the General Programming Boards category; Hello people, Just a quick trivial question: What is the result of the following code?? Code: #define PP(n) (1 << ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Leftshift???

    Hello people,

    Just a quick trivial question:

    What is the result of the following code??

    Code:
    #define PP(n) (1 << (n))
    I can't fully understand this.

    I now that defines a new array with 'n' capacity but I can't get the result of the 1<<(n).

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    It calculates 2**n. (2 to the nth power)

    edit - it doesn't "calculate" it, it "defines" it.
    Last edited by Dino; 02-05-2008 at 12:39 PM. Reason: clarification
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    1<<0 == 0x0001
    1<<1 == 0x0002
    1<<16 == 0x0010

    etc
    it creates the number in binary form of 10000000
    where there is n (binary) zeroes after 1
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  4. #4
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    1<<16 == 0x0010
    Bit off there: 1<<16 == 0x10000
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    I thought so too initially, but he really isn't. Note that he shows 1 << 1 to be 0x0002. It's not the preferred format, but it is not incorrect.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Todd Burch View Post
    I thought so too initially, but he really isn't. Note that he shows 1 << 1 to be 0x0002. It's not the preferred format, but it is not incorrect.
    And 1 << 16 is 0x10000.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    He used one digit to represent each byte in a 32 bit word. So, 1<<15 would have been 0x000F.

    I've explained it twice - no more sticking up for him!!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Yeah, the format he used sucks. I take it back. It's so wrong it's totally unclear. Either that, or or just dipped back into 16 bit harware and showed a 2-byte word.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  9. #9
    Registered User
    Join Date
    Feb 2008
    Posts
    2

    Thumbs up

    Thank you very much for all the replies!!!


    That makes sense now!

    Although, I should have tried a bit harder to understand it and not be impatient.

  10. #10
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,473
    Quote Originally Posted by Todd Burch View Post
    So, 1<<15 would have been 0x000F.
    No, never

    0xF in binary is 1111
    So it cannot be represented in a form of 1<<n

    with 1<<16 I was wrong - sorry
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Quote Originally Posted by vart View Post
    No, never

    0xF in binary is 1111
    So it cannot be represented in a form of 1<<n
    Brain fart me too. Right.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  12. #12
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,237
    Quote Originally Posted by arkroan View Post
    What is the result of the following code??

    Code:
    #define PP(n) (1 << (n))
    The result is a macro PP() which replaces its lexical argument "n" with the expression "(1 << (n))"

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21