Unsigned Shift Of Byte

Is there an Error with Unsigned Shift in Java ???

look at this:

if you make an >>> Operation to a Byte:

for (byte sourc = Byte.MIN_VALUE; sourc < Byte.MAX_VALUE; sourc++) {
cat.info(sourc + " >>> 1 = "+ (byte)(sourc >> 1));
}

this seems to not really work:

look:

-126>>>1 = -63

if you make this manually, you get this:

-126 = (bin) 1000 0010
| |
| +-------> the value 1000 0000 = -128
+--------------> this is the minus

if you shift it RIGHT UNSIGNED, it should be:

  1. 0010 >>> 1 = 0100 0001 = +65

this is clear: look:

(original Java) should be!!
  1. >>> 1 = 60 60 (OK)
  2. >>> 1 = 61 61 (OK)
  3. >>> 1 = 61 61 (OK)
  4. >>> 1 = 62 62 (OK)
  5. >>> 1 = 62 62 (OK)
  6. >>> 1 = 63 63 (OK)
  7. >>> 1 = 63 63 (OK)
-128 >>> 1 =-64 64
-127 >>> 1 =-64 64
-126 >>> 1 =-63 65
-125 >>> 1 =-63 65
-124 >>> 1 =-62 66
-123 >>> 1 =-62 66
-122 >>> 1 =-61 67
-121 >>> 1 =-61 67

now, java does the following, due to the Java Language Spec:

the >>> Operator makes a cast to int. then the result is processed, and we do a cast back to byte.

with this in Mind, we can see the truth:

(original Java) should be!!
  1. >>> 1 = 60 60 (OK)
  2. >>> 1 = 61 61 (OK)
  3. >>> 1 = 61 61 (OK)
  4. >>> 1 = 62 62 (OK)
  5. >>> 1 = 62 62 (OK)
  6. >>> 1 = 63 63 (OK)
  7. >>> 1 = 63 63 (OK)
-128 >>> 1 = 2147483584 64
-127 >>> 1 = 2147483584 64
-126 >>> 1 = 2147483585 65
-125 >>> 1 = 2147483585 65
-124 >>> 1 = 2147483586 66
-123 >>> 1 = 2147483586 66
-122 >>> 1 = 2147483587 67
-121 >>> 1 = 2147483587 67

now we see: the unsigned shift Operator DOES really shift a negative Value, without care about the Signs bit, so all Negative does get positive!!!

the problem is the cast to byte!!!


this is a possible Solution:

for (source = Byte.MIN_VALUE; source < Byte.MAX_VALUE; source++) {
cat.info(source + " >>> 1 = "+ ( (byte)(source >> 1) & Byte._0111_1111 ) );
}

VisualC++ has similar shifting problems, requiring explicit casts to signedness and size to do it correctly. Perhaps the same is true of Java?


But this is not a problem since, according to Java specification, the byte is augmented to an int before shifting.


Yes, that's true, if you say: the Java language works correctly, because of the Specification.

You are Right!! This is not a Bug!

But, that's Useless if you need a really Unsigned Shift for a Byte!!


I thought everybody knew that A "FEATURE" IS A DOCUMENTED BUG.


Not a bug, but still a problem. The bit-fiddling code I write in Java is a mixture of occasional obscure type-promotion errors and an otherwise ridiculously defensive explicit-casting programming style. Much more so than in C, where you have unsigned types. I've recently found some very simple CommonLisp primitives for manipulating sub-bytes of integers extremely handy - it would be great to see them added to other languages. Details at http://www.lispworks.com/reference/HyperSpec/Body/12_aacb.htm -- LukeGorrie


Here's a Way to do it:

/**
int unsigned(byte b) {
return ((int)b)&0xFF; //so, only the lowest Byte of the int is taken.
}

Now you can:

byte b = (byte)128;
x = unsigned(b) >>> 1

See ArithmeticShiftingConsideredHarmful