# only for wise

krish bajaj

Ranch Hand

Posts: 31

wise owen

Ranch Hand

Posts: 2023

posted 9 years ago

"<<" op1 << op2 Shifts bits of op1 left by distance op2; fills with 0 bits on the right side

">>" op1 >> op2 Shifts bits of op1 right by distance op2; fills with highest (sign) bit on the left side

">>>" op1 >>> op2 Shifts bits of op1 right by distance op2; fills with 0 bits on the left side

Let's say we have positive integer number 12 whose binary equivalent is 00000000 00000000 00000000 00001100 (binary for 32 bits).

Remember that the type operands (such as byte, short, etc) are implicitly promoted to the int type

before any shift operators are applied.

1)

00000000 00000000 00000000 00001100 >> 2.

The result is

xx000000 00000000 00000000 00000011. We left 2 "x" bits need to fill. When shifting right ( >> ) the leftmost bits exposed by the right shift are filled in with previous contents of the leftmost bit.

Therefore, we need to fill 0, the final result is

00000000 00000000 00000000 00000011

2)

00000000 00000000 00000000 00001100 << 2.

The result is

00000000 00000000 00000000 001100xx. We left 2 "x" bits need to fill. When you shift left ( << ) the void left behind by the shift is filled by zero's.

Therefore, we need to fill 0, the final result is

00000000 00000000 00000000 00110000

Let's take a look a negative number -12 whose binary equivalent is 11111111 11111111 11111111 11110100 (32 bits)

1)

11111111 11111111 11111111 11110100 >> 2.

The result is

xx111111 11111111 11111111 11111101. We left 2 "x" bits need to fill. When shifting right ( >> ) the leftmost bits exposed by the right shift are filled in with previous contents of the leftmost bit.

Therefore, we need to fill 0, the final result is

11111111 11111111 11111111 11111101

2)

11111111 11111111 11111111 11110100 << 2.

The result is

11111111 11111111 11111111 110100xx. We left 2 "x" bits need to fill. When you shift left ( << ) the void left behind by the shift is filled by zero's.

Therefore, we need to fill 0, the final result is

11111111 11111111 11111111 11010000

Note that right shifting ( >> ) always preserves the sign of the original number i.e. to say that a negative number will stay negative while a positive number will stay positive after a right shift ( >> ) .

BUT, the >>> unsigned right shift operator always fill zero's & only zero's no matter poistive and negative number.

1)

00000000 00000000 00000000 00001100 >>> 2.

The result is

00000000 00000000 00000000 00000011.

2)

11111111 11111111 11111111 11110100 >>> 2.

The result is

00111111 11111111 11111111 11111101

What is the range for op2?

For int type:

1)

00000000 00000000 00000000 00001100 >> 32 (or <<32).

The result is

00000000 00000000 00000000 00001100.

2)

11111111 11111111 11111111 11110100 >> 32 (or <<32).

The result is

11111111 11111111 11111111 11110100

[ June 19, 2006: Message edited by: wise owen ]

">>" op1 >> op2 Shifts bits of op1 right by distance op2; fills with highest (sign) bit on the left side

">>>" op1 >>> op2 Shifts bits of op1 right by distance op2; fills with 0 bits on the left side

Let's say we have positive integer number 12 whose binary equivalent is 00000000 00000000 00000000 00001100 (binary for 32 bits).

Remember that the type operands (such as byte, short, etc) are implicitly promoted to the int type

before any shift operators are applied.

1)

00000000 00000000 00000000 00001100 >> 2.

The result is

xx000000 00000000 00000000 00000011. We left 2 "x" bits need to fill. When shifting right ( >> ) the leftmost bits exposed by the right shift are filled in with previous contents of the leftmost bit.

Therefore, we need to fill 0, the final result is

00000000 00000000 00000000 00000011

2)

00000000 00000000 00000000 00001100 << 2.

The result is

00000000 00000000 00000000 001100xx. We left 2 "x" bits need to fill. When you shift left ( << ) the void left behind by the shift is filled by zero's.

Therefore, we need to fill 0, the final result is

00000000 00000000 00000000 00110000

Let's take a look a negative number -12 whose binary equivalent is 11111111 11111111 11111111 11110100 (32 bits)

1)

11111111 11111111 11111111 11110100 >> 2.

The result is

xx111111 11111111 11111111 11111101. We left 2 "x" bits need to fill. When shifting right ( >> ) the leftmost bits exposed by the right shift are filled in with previous contents of the leftmost bit.

Therefore, we need to fill 0, the final result is

11111111 11111111 11111111 11111101

2)

11111111 11111111 11111111 11110100 << 2.

The result is

11111111 11111111 11111111 110100xx. We left 2 "x" bits need to fill. When you shift left ( << ) the void left behind by the shift is filled by zero's.

Therefore, we need to fill 0, the final result is

11111111 11111111 11111111 11010000

Note that right shifting ( >> ) always preserves the sign of the original number i.e. to say that a negative number will stay negative while a positive number will stay positive after a right shift ( >> ) .

BUT, the >>> unsigned right shift operator always fill zero's & only zero's no matter poistive and negative number.

1)

00000000 00000000 00000000 00001100 >>> 2.

The result is

00000000 00000000 00000000 00000011.

2)

11111111 11111111 11111111 11110100 >>> 2.

The result is

00111111 11111111 11111111 11111101

What is the range for op2?

If the promoted type of the left-hand operand is int, only the five lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x1f. The shift distance actually used is therefore always in the range 0 to 31, inclusive.

If the promoted type of the left-hand operand is long, then only the six lowest-order bits of the right-hand operand are used as the shift distance. It is as if the right-hand operand were subjected to a bitwise logical AND operator & (�15.22.1) with the mask value 0x3f. The shift distance actually used is therefore always in the range 0 to 63, inclusive.

For int type:

1)

00000000 00000000 00000000 00001100 >> 32 (or <<32).

The result is

00000000 00000000 00000000 00001100.

2)

11111111 11111111 11111111 11110100 >> 32 (or <<32).

The result is

11111111 11111111 11111111 11110100

[ June 19, 2006: Message edited by: wise owen ]

Herb Tybur

Greenhorn

Posts: 19

posted 9 years ago

I think we're all glad to have wise owen posting here. But these are public forums, and I don't think anyone should be "discouraged" from participating. Remember, if a response isn't clear, you can always ask for clarification.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." *~Joe Strummer*

sscce.org