# 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. ## 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. It calculates 2**n. (2 to the nth power)

edit - it doesn't "calculate" it, it "defines" it.

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

4. 1<<16 == 0x0010
Bit off there: 1<<16 == 0x10000

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

6. Originally Posted by Todd Burch
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

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

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

9. 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. Originally Posted by Todd Burch
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

11. Originally Posted by vart
No, never

0xF in binary is 1111
So it cannot be represented in a form of 1<<n
Brain fart me too. Right.

12. Originally Posted by arkroan
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))"