# Shift operator

Twinkle

Greenhorn

Posts: 2

posted 11 years ago

- 0

Hey All,

Here is a question found in a java test.

The following code will print

1: int i = 1;

2: i <<= 31;

3: i >>= 31;

4: i >>= 1;

5:

6: int j = 1;

7: j >>= 31;

8: j >>= 31;

9:

10: System.out.println("i = " +i );

11: System.out.println("j = " +j);

A) i = 1

j = 1

B) i = -1

j = 1

C) i = 1

j = -1

D) i = -1

j = 0

I thought the answer was i=0, j=1. But there was no choice like that.

The answer is D.

Can anybody give me an explanation?

Thanks,

Twinkle

Here is a question found in a java test.

The following code will print

1: int i = 1;

2: i <<= 31;

3: i >>= 31;

4: i >>= 1;

5:

6: int j = 1;

7: j >>= 31;

8: j >>= 31;

9:

10: System.out.println("i = " +i );

11: System.out.println("j = " +j);

A) i = 1

j = 1

B) i = -1

j = 1

C) i = 1

j = -1

D) i = -1

j = 0

I thought the answer was i=0, j=1. But there was no choice like that.

The answer is D.

Can anybody give me an explanation?

Thanks,

Twinkle

C. Nimo

Ranch Hand

Posts: 82

posted 11 years ago

- 0

it's all about size and representation.

an int value is represented with 32bits. however, it is represented by a notation called 2's complement. basically, this means that if you have a '1' in the leftmost bit of a number, this is a negative number.

now, i=1 means that it is represented by 31 consecutive zeros, and then a '1'. if you shift that 31 bits to the left, you end up with a '1' at the leftmost bit, which makes it a negative number.

the >> shift right, copies the leftmost bit (the 'sign' bit) - so, when you later shift your number 31 bits to the right, you get a binary represantation of all '1'. this is actually -1 in 2's complement notation.

the last shift doesn't matter, since all the bits are 1 anyway, and the >> shift copies the sign bit. so you still have 32 bits, all of them '1'.

now about the j var - this is initialized to 31 bits all zeros, and a 1 at the rightmost bit. the >> of 31 bits drops the 1 over the edge, and you get the binary representation of all zeros. any shift on that will leave you again with the value of zero.

nimo.

an int value is represented with 32bits. however, it is represented by a notation called 2's complement. basically, this means that if you have a '1' in the leftmost bit of a number, this is a negative number.

now, i=1 means that it is represented by 31 consecutive zeros, and then a '1'. if you shift that 31 bits to the left, you end up with a '1' at the leftmost bit, which makes it a negative number.

the >> shift right, copies the leftmost bit (the 'sign' bit) - so, when you later shift your number 31 bits to the right, you get a binary represantation of all '1'. this is actually -1 in 2's complement notation.

the last shift doesn't matter, since all the bits are 1 anyway, and the >> shift copies the sign bit. so you still have 32 bits, all of them '1'.

now about the j var - this is initialized to 31 bits all zeros, and a 1 at the rightmost bit. the >> of 31 bits drops the 1 over the edge, and you get the binary representation of all zeros. any shift on that will leave you again with the value of zero.

nimo.

Twinkle

Greenhorn

Posts: 2

posted 11 years ago

- 0

Thanks Nimo,

i got the "j-part". But i am still having doubts about the "i-part". What do you mean by >> operator copies the leftmost bit. I thought >> operator moves all the bits by one position to the right.

So if i= 1 which means i = 0000 0000 0000 0000 0000 0000 0000 0001 in binary, after doing i <<= 31,

i=1000 0000 0000 0000 0000 0000 0000 0000

Now if I do i >>= 1;

I thought the value of i should be 0100 0000 0000 0000 0000 0000 0000 0000

Can you clarify it?

Thanks

Twinkle

i got the "j-part". But i am still having doubts about the "i-part". What do you mean by >> operator copies the leftmost bit. I thought >> operator moves all the bits by one position to the right.

So if i= 1 which means i = 0000 0000 0000 0000 0000 0000 0000 0001 in binary, after doing i <<= 31,

i=1000 0000 0000 0000 0000 0000 0000 0000

Now if I do i >>= 1;

I thought the value of i should be 0100 0000 0000 0000 0000 0000 0000 0000

Can you clarify it?

Thanks

Twinkle

Dirk Schreckmann

Sheriff

Posts: 7023

posted 11 years ago

- 0

Twinkle Twinkle,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!

C. Nimo

Ranch Hand

Posts: 82

posted 11 years ago

- 0

There are two kinds of shift-right operators in Java - signed, and unsigned.

a signed shift, which is denoted by >>, shifts the bits right, keeping the sign. the unsigned shift-right, denoted by >>>, inserts zeros on the left.

now, when you have a negative number, which means that it has a leftmost bit with the value of '1' - a signed shift-right will not insert zeros on the left, it will insert '1's.

nimo.

a signed shift, which is denoted by >>, shifts the bits right, keeping the sign. the unsigned shift-right, denoted by >>>, inserts zeros on the left.

now, when you have a negative number, which means that it has a leftmost bit with the value of '1' - a signed shift-right will not insert zeros on the left, it will insert '1's.

nimo.