Hung Chang

Greenhorn

Posts: 16

Johannes de Jong

tumbleweed

Bartender

Bartender

Posts: 5089

posted 15 years ago

The word unsigned says it all. This means that the sign bit which is the left-most bit (also called high-order bit) is not included in the shift.

**Unsigned Right Shift**

The word unsigned says it all. This means that the sign bit which is the left-most bit (also called high-order bit) is not included in the shift.

Michael Bruesch

Ranch Hand

Posts: 158

posted 15 years ago

If you mean Shift-Right-With-Zero-Fill, the >>> operator, then that's probably a trick question. If you shift an integer 0 or any multiple of 32 bits to the right, you will get the same number again. Take this example:

int x = -1;

x = x >>> 0; // x is still -1

x = x >>> 32 // x is still -1

And so on. Now honestly I'm not sure why you get back to the same number after 32, unless it somehow subtracts the highest multiple of 32 from the bit shift number, then shifts??? Dangit, now I have a question...

------------------

Michael J Bruesch

Codito, ergo sum...

http://www.geocities.com/mjbruesch

int x = -1;

x = x >>> 0; // x is still -1

x = x >>> 32 // x is still -1

And so on. Now honestly I'm not sure why you get back to the same number after 32, unless it somehow subtracts the highest multiple of 32 from the bit shift number, then shifts??? Dangit, now I have a question...

------------------

Michael J Bruesch

Codito, ergo sum...

*I code, therefore I am.*http://www.geocities.com/mjbruesch

Michael J Bruesch<br /><i>I code, therefore I am.</i>

Michael Bruesch

Ranch Hand

Posts: 158

posted 15 years ago

Alright, found it.

That's why a 32 shift distance is the same as a 0 shift distance.

------------------

Michael J Bruesch

Codito, ergo sum...

http://www.geocities.com/mjbruesch

The number of bits shifted is always in the range modulus 32 for an int value, and in the range modulus 64 for a long value.

A Programmer's Guide to Java Certification by Khalid Mughal

That's why a 32 shift distance is the same as a 0 shift distance.

------------------

Michael J Bruesch

Codito, ergo sum...

*I code, therefore I am.*http://www.geocities.com/mjbruesch

Michael J Bruesch<br /><i>I code, therefore I am.</i>

Johannes de Jong

tumbleweed

Bartender

Bartender

Posts: 5089

Hung Chang

Greenhorn

Posts: 16

Johannes de Jong

tumbleweed

Bartender

Bartender

Posts: 5089

Marilyn de Queiroz

Sheriff

Posts: 9080

12

posted 15 years ago

The key is "always"

Unsigned Right Shift on a Negative Integer

a Positive Integer.

The answer is false.

Try this:

output = -64

According to RHE, "shifts of ints use only the low-order 5 bits". This can produce an anomalous result.

Basically you do modulo on the right operand, in this case 32 % 32 = 0, so no shift takes place and the result is still negative.

Unsigned Right Shift on a Negative Integer

**always**returnsa Positive Integer.

The answer is false.

Try this:

output = -64

According to RHE, "shifts of ints use only the low-order 5 bits". This can produce an anomalous result.

Basically you do modulo on the right operand, in this case 32 % 32 = 0, so no shift takes place and the result is still negative.

JavaBeginnersFaq

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

Consider Paul's rocket mass heater. |