Thread: Leftshift???

  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
    Chappell Hill, Texas
    Posts
    2,332
    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 01:39 PM. Reason: clarification
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Chappell Hill, Texas
    Posts
    2,332
    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!!
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    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.
    Mainframe assembler programmer by trade. C coder when I can.

  12. #12
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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