posted 17 years ago

What don't you understand?

Marilyn

Marilyn

JavaBeginnersFaq

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt

posted 17 years ago

the shift operators..

1. >> signed right shift : the left most bit is inserted.

2. << signed left shift : 0 is inserted from the right

3. >>> unsigned right shift: 0 is inserted from the left

now u need to understand how we represent a decimal no. in binary. we know a int is 32 bitsa in java. so there will be 32

bits in any decimal no.

say 18 : 0000 0000 0000 0000 0000 0000 0001 0010

now i hope u understand how i represented the no. 18 in binary..

u also need to understand what is meant by signed and unsigned shift..

the left most bit is meant for sign and if it is 1 the no. is negative.

if the bit is 0 the no is positive.

now what i mean by signed shift is that the sign of the no. will be maintained.

i.e no matter how many times u shift right with << or >> the sign will be preserved.

>>> instead inserts a 0 on the left ..so if there is a 1 on the left most bit, the no. is negative, once u insert the 0 , the no changes sign from -ve to +ve.

one imp thing.

a>>32 will not shift the no at all why? because the right hand side is divided by 32 and the no is shifted by the remainder.thus the shift is actually

a>>b%32 in real sense.

1. >> signed right shift : the left most bit is inserted.

2. << signed left shift : 0 is inserted from the right

3. >>> unsigned right shift: 0 is inserted from the left

now u need to understand how we represent a decimal no. in binary. we know a int is 32 bitsa in java. so there will be 32

bits in any decimal no.

say 18 : 0000 0000 0000 0000 0000 0000 0001 0010

now i hope u understand how i represented the no. 18 in binary..

u also need to understand what is meant by signed and unsigned shift..

the left most bit is meant for sign and if it is 1 the no. is negative.

if the bit is 0 the no is positive.

now what i mean by signed shift is that the sign of the no. will be maintained.

i.e no matter how many times u shift right with << or >> the sign will be preserved.

>>> instead inserts a 0 on the left ..so if there is a 1 on the left most bit, the no. is negative, once u insert the 0 , the no changes sign from -ve to +ve.

one imp thing.

a>>32 will not shift the no at all why? because the right hand side is divided by 32 and the no is shifted by the remainder.thus the shift is actually

a>>b%32 in real sense.

Anonymous

Ranch Hand

Posts: 18944

posted 17 years ago

Azam,

I suggest that you read up on binary numbers paying special attention to 2's complement representation of numbers.

The shift operators will explain themselves after that.

Inside a computer, all numbers (indeed all codes, etc. also) are stored as binary numbers. An int will consist of 32 bits. For simplicity's sake, we will assume an integer of 8 bits wide.

int i = 5;

here 5 will be represented as 0000 0101

Got this? (If you don't then I'm afraid its the binary number system book for you.)

i>>1 means right shift by 1

so the new i will be 0000 0010 = decimal 2

i=5; i>>2;

the new i now will be 0000 0001 = decimal 1

In simple terms, a right shift will divide by two and throw away the remainder and a left shift will multiply by two.(In fact, this is exactly how our computer internally performs multiplication and division, though in real life there are a lot more complications)

The unsigned shift is a lot trickier.

First the basics: The left most bit is used to indicate the sign of the number. 0=positive and 1 = negative. However, when the number is negative, the 2's complement of the number is stored.

Normally, a shift retains the sign. ie., if it was positive to begin with, it will continue to be positive and if it was negative to begin with, the left most bit will continue to be 1 even if we shift right.

With the unsigned shift, the left most bit is simple replaced with 0 regardless of the sign of the number.

Hope things are clearer with this

Good Luck

Ambrose

I suggest that you read up on binary numbers paying special attention to 2's complement representation of numbers.

The shift operators will explain themselves after that.

Inside a computer, all numbers (indeed all codes, etc. also) are stored as binary numbers. An int will consist of 32 bits. For simplicity's sake, we will assume an integer of 8 bits wide.

int i = 5;

here 5 will be represented as 0000 0101

Got this? (If you don't then I'm afraid its the binary number system book for you.)

i>>1 means right shift by 1

so the new i will be 0000 0010 = decimal 2

i=5; i>>2;

the new i now will be 0000 0001 = decimal 1

In simple terms, a right shift will divide by two and throw away the remainder and a left shift will multiply by two.(In fact, this is exactly how our computer internally performs multiplication and division, though in real life there are a lot more complications)

The unsigned shift is a lot trickier.

First the basics: The left most bit is used to indicate the sign of the number. 0=positive and 1 = negative. However, when the number is negative, the 2's complement of the number is stored.

Normally, a shift retains the sign. ie., if it was positive to begin with, it will continue to be positive and if it was negative to begin with, the left most bit will continue to be 1 even if we shift right.

With the unsigned shift, the left most bit is simple replaced with 0 regardless of the sign of the number.

Hope things are clearer with this

Good Luck

Ambrose