This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.

hi, even i am preparing for scjp1.5. if anyone finds there is anything wrong with my answer then please suggest me. i am thinking this is the way the left shift operator works. first convert the decimal into binary form. as its an int, it looks like decimal 37=0000 0000 0000 0000 0000 0000 0010 0101(binary int form) now start shifting each bit one by one from their original position to 50th position.since the size of an int is 32bit,after reaching the rightmost bit the count starts again from leftmost position.i,e the number at 50th position will appear at 18th position(32+18=50).so 37<<50 gives 9699328.

Imagine a magical door, that when you come out from one side you appear at the other.

Now imagine that when you move a bit beyond the bit-scope of an integer it appears at the beging of the scope back again. (This is just to explain, it does not work that way)

So, as you cannot move 50 bits to the left in integer, because integers just have 32 bits, first you move them 32 bits to the left, and they start appearing a the begging all over again. Then you move them the remaining 18 bits. So at the end, you just move bits 18 bits to the left.

So, when the shift number is bigger than 32 you simply rest that number from 32.

That's to say:

If you see the results at a bit level you will see that the bits were moved just 18 bits to the left

Hope it helps! [ August 22, 2005: Message edited by: Edwin Dalorzo ]

Look at the lefthand operand. Decide whether it is an int or a long. If it is an int take the rightmost five bits of the righthand operand. If it is a long take the rightmost six bits of the righthand operand. Treat the five or six bits as a positive number, s say. Now shift the bits of the lefthand operand to the left ( << ) or to the right by s bits ( for >> sign fill, for >>> zero fill ). That's all there is to it. [ August 22, 2005: Message edited by: Barry Gaunt ]

For an int when we shift bit by more than 32 we end up shifting bits by n%32 i.e x >> 34 is equal to x >> 2 .

What is the result of 8 << -1 ?

Cheers Preparing for SCJP1.4

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

posted

0

Originally posted by Kanchan K Bulbule:

What is the result of 8 << -1 ?

The LHS is 8, an int. So we take the rightmost five bits of -1. That is 11111. As a positive number 11111 is 31. So shift 1000 (8) by 31 bits to the left.

And you get?

How about 8L << -1? [ August 23, 2005: Message edited by: Barry Gaunt ]

Kanchan K Bulbule
Greenhorn

Joined: Jul 19, 2005
Posts: 20

posted

0

Thanks for quick tip !!

8 << -1 is equal to 8 << 31 = 0

and ----------------------- How about 8L << -1? -----------------------

Being a long we will consider 6 MSB of -1 ie 111111 = 127 , so it will be 8 << 127 that in turn will be 8L << 63 = 0

Correct me if I have misunderstood !

Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729

posted

0

Originally posted by Kanchan K Bulbule: Thanks for quick tip !!

8 << -1 is equal to 8 << 31 = 0

and ----------------------- How about 8L << -1? -----------------------

Being a long we will consider 6 MSB of -1 ie 111111 = 127 , so it will be 8 << 127 that in turn will be 8L << 63 = 0

Correct me if I have misunderstood !

Your final answer of zero is correct, but binary 111111 is not 127, it is 63.

For a shock try 1 << -1 and 1L << -1.

As Bert has said this type of question is not in SCJP 5.0, but a programmer should be aware of the suprises that Java holds in store.

shashikanth penumadula
Ranch Hand

Joined: May 26, 2005
Posts: 32

posted

0

hi in my reply, there was a mistake. replace the words rightmost with leftmost and leftmost with rightmost.

Kanchan K Bulbule
Greenhorn

Joined: Jul 19, 2005
Posts: 20

posted

0

1<<-1 will be equal to 1<<31 ie . lsb (ie 1) will shift 31 places and now become 1000 0000 0000 0000 0000 0000 0000 0000 i.e -2 raised to 31 and similarly 1L<<-1 will be equal to 1L<<63 which will be -2 raised to 63