# binary shift operations with negative RHS ?

sivaram subram

Greenhorn

Posts: 9

Manfred Leonhardt

Ranch Hand

Posts: 1492

posted 15 years ago

Hi Sivaram,

When the value is positive we can say that for integers the shift amount is:

range modulus 32

and for long:

range modulus 64.

When the value is negative we can say the following for integers:

(32 - range) modulus 32

and for long:

(64 - range) modulus 64.

Therefore, we can say that the following 2 statements are equivalent:

1 << -2;

1 << 30;

Regards,

Manfred.

When the value is positive we can say that for integers the shift amount is:

range modulus 32

and for long:

range modulus 64.

When the value is negative we can say the following for integers:

(32 - range) modulus 32

and for long:

(64 - range) modulus 64.

Therefore, we can say that the following 2 statements are equivalent:

1 << -2;

1 << 30;

Regards,

Manfred.

sing

Ranch Hand

Posts: 121

sing

Ranch Hand

Posts: 121

posted 15 years ago

Hi Manfred,

Can you give more examples or any references of this negative shifting? Thank You.

Regards,

Steffy

Can you give more examples or any references of this negative shifting? Thank You.

Regards,

Steffy

Originally posted by Manfred Leonhardt:

Hi Sivaram,

When the value is positive we can say that for integers the shift amount is:

range modulus 32

and for long:

range modulus 64.

When the value is negative we can say the following for integers:

(32 - range) modulus 32

and for long:

(64 - range) modulus 64.

Therefore, we can say that the following 2 statements are equivalent:

1 << -2;

1 << 30;

Regards,

Manfred.

posted 15 years ago

Here a just a couple of threads that you can refer to.

http://www.javaranch.com/ubb/Forum1/HTML/001638.html http://www.javaranch.com/ubb/Forum33/HTML/002836.html

This site has a search facility that I suggest you try to use before posting a question.

Thanks,

Junilu

http://www.javaranch.com/ubb/Forum1/HTML/001638.html http://www.javaranch.com/ubb/Forum33/HTML/002836.html

This site has a search facility that I suggest you try to use before posting a question.

Thanks,

Junilu

Junilu - [How to Ask Questions] [How to Answer Questions]