Sherry Jacob

Ranch Hand

Posts: 128

shashikanth penumadula

Ranch Hand

Posts: 32

posted 12 years ago

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.

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.

Edwin Dalorzo

Ranch Hand

Posts: 961

posted 12 years ago

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 ]

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 ]

Barry Gaunt

Ranch Hand

Posts: 7729

posted 12 years ago

Look at the lefthand operand. Decide whether it is an

[ August 22, 2005: Message edited by: Barry Gaunt ]

**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 ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch

Getting someone to think and try something out is much more useful than just telling them the answer.

Bert Bates

author

Sheriff

Sheriff

Posts: 8945

17

Kanchan K Bulbule

Greenhorn

Posts: 20

Barry Gaunt

Ranch Hand

Posts: 7729

posted 12 years ago

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 ]

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 ]

Getting someone to think and try something out is much more useful than just telling them the answer.

Kanchan K Bulbule

Greenhorn

Posts: 20

posted 12 years ago

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 !

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

Posts: 7729

posted 12 years ago

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.

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.

Getting someone to think and try something out is much more useful than just telling them the answer.

shashikanth penumadula

Ranch Hand

Posts: 32