posted 12 years ago

Hi,

For shift operators manipulating positive numbers we can use the short-cut formula,

For example:

for signed right shift: x >> 3, can be solved using the formula x * (2 raised to 3)

for signed left shift: x << 3, can be solved using the formula x / (2 raised to 3)

Is there a short-cut formula to determine the following:

a. unsigned right shift (>>> ?

b. signed right or left shift manipulating negative numbers?

i.e. -5 >> 3 or -5 << 3 or -5 >> -3 or -5 << -3

Thanks in advance!

For shift operators manipulating positive numbers we can use the short-cut formula,

For example:

for signed right shift: x >> 3, can be solved using the formula x * (2 raised to 3)

for signed left shift: x << 3, can be solved using the formula x / (2 raised to 3)

Is there a short-cut formula to determine the following:

a. unsigned right shift (>>> ?

b. signed right or left shift manipulating negative numbers?

i.e. -5 >> 3 or -5 << 3 or -5 >> -3 or -5 << -3

Thanks in advance!

"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)

posted 12 years ago

unsigned right shift operator divides the left hand operand to 2 to the power of right hand operand( Only when the left hand operand is positive value)

signed left always multiplies the left hand operand by 2 to the power of right hand operand.

signed right ( >>) divides the left hand operand by 2 to the power of right hand operand [except -1 >> 1 = -1]

Originally posted by Chard Rex:

a. unsigned right shift (>>> ?

unsigned right shift operator divides the left hand operand to 2 to the power of right hand operand( Only when the left hand operand is positive value)

b. signed right or left shift manipulating negative numbers?

i.e. -5 >> 3 or -5 << 3 or -5 >> -3 or -5 << -3

signed left always multiplies the left hand operand by 2 to the power of right hand operand.

signed right ( >>) divides the left hand operand by 2 to the power of right hand operand [except -1 >> 1 = -1]

Manivannan

Richard Rex

Ranch Hand

Posts: 100

posted 12 years ago
"If you fail to plan, you plan to fail."<br /> <br />Chad<br />SCJP 1.4<br />SCWCD 1.4<br />SCBCD 1.3<br />SCDJWS (next)

Hi Manivannan Palanichamy,

Thanks for your reply.

I'm just with this..

public class ShiftOp{

public static void main(String[] args){

System.out.println(-6 >> -1);

System.out.println(-6 >> -2);

System.out.println(-6 >> -3);

System.out.println(-6 >> -4);

System.out.println(-6 >> -5);

}

}

The output for this program are all -1.

How do we do this using the short-cut formula?

Thanks for your reply.

I'm just with this..

public class ShiftOp{

public static void main(String[] args){

System.out.println(-6 >> -1);

System.out.println(-6 >> -2);

System.out.println(-6 >> -3);

System.out.println(-6 >> -4);

System.out.println(-6 >> -5);

}

}

The output for this program are all -1.

How do we do this using the short-cut formula?

Mani vannan

Ranch Hand

Posts: 185

posted 12 years ago

Hi,

public class ShiftOp{

public static void main(String[] args){

System.out.println(-6 >> -1);

System.out.println(-6 >> -2);

System.out.println(-6 >> -3);

System.out.println(-6 >> -4);

System.out.println(-6 >> -5);

}

}

The output for this program are all -1.

How do we do this using the short-cut formula?

Does anyone know the short-cut formula here?

public class ShiftOp{

public static void main(String[] args){

System.out.println(-6 >> -1);

System.out.println(-6 >> -2);

System.out.println(-6 >> -3);

System.out.println(-6 >> -4);

System.out.println(-6 >> -5);

}

}

The output for this program are all -1.

How do we do this using the short-cut formula?

Does anyone know the short-cut formula here?

Richard Rex

Ranch Hand

Posts: 100

posted 12 years ago

Hope i am not irritating you with this answer.

1) Eventhough you have provided the negative number after the shift operator, it is getting considered as positive(considered as unsigned). In a 8-bit signed integer representation -1 is (1111 1111). In unsigned it becomes 256.

So if we give -6 >> -1 is equal to -6 >> 256(consider the equivalent in 32 bit system i.e. 65536). So in a 8 bit system, if i shift it by 256 times on the right side(with sign preserved), then it becomes -1 (for -ve) and 0(for +ve).

So the formula may be written like this.

(In a 32 bit representation)

x >> y = (x / 2^y) (if y < 32)

= 0 (if x is +ve and y >=32 or y<0)

= -1 (if x is -ve and y>=32 or y<0)

the point to be remembered is that y will be considered always positive. So any negative number in signed system will become a positive number on the higher end.

I haven't checked my observation with programs. Please inform me, if you find some mistakes.

Thanks,

Gokul.

1) Eventhough you have provided the negative number after the shift operator, it is getting considered as positive(considered as unsigned). In a 8-bit signed integer representation -1 is (1111 1111). In unsigned it becomes 256.

So if we give -6 >> -1 is equal to -6 >> 256(consider the equivalent in 32 bit system i.e. 65536). So in a 8 bit system, if i shift it by 256 times on the right side(with sign preserved), then it becomes -1 (for -ve) and 0(for +ve).

So the formula may be written like this.

(In a 32 bit representation)

x >> y = (x / 2^y) (if y < 32)

= 0 (if x is +ve and y >=32 or y<0)

= -1 (if x is -ve and y>=32 or y<0)

the point to be remembered is that y will be considered always positive. So any negative number in signed system will become a positive number on the higher end.

I haven't checked my observation with programs. Please inform me, if you find some mistakes.

Thanks,

Gokul.

Gokul Somasundaram

Greenhorn

Posts: 20

posted 12 years ago

Remember that an & bitmask is applied to the right operand of the shift operators.

For int numbers, value of the mask is 0x1f (11111) (so the shift opertaion is always in a range 0-31)

a >> b == a >> (b & 0x1f)

That's not true :

For example

x >> Integer.MIN_VALUE = x

(Integer.MIN_VALUE =-2147483648 = -2^32)

cause Integer.MIN_VALUE & 0x1f = 0

For int numbers, value of the mask is 0x1f (11111) (so the shift opertaion is always in a range 0-31)

a >> b == a >> (b & 0x1f)

x >> y = (x / 2^y) (if y < 32)

= 0 (if x is +ve and y >=32 or y<0)

= -1 (if x is -ve and y>=32 or y<0)

That's not true :

For example

x >> Integer.MIN_VALUE = x

(Integer.MIN_VALUE =-2147483648 = -2^32)

cause Integer.MIN_VALUE & 0x1f = 0

Regards,<br />Seb<br /> <br />SCJP 1.4

Consider Paul's rocket mass heater. |