diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java index 36e6952f0..3790111d6 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TCharacter.java @@ -734,6 +734,6 @@ public class TCharacter extends TObject implements TComparable { } public static char reverseBytes(char ch) { - return (char) ((ch >> 8) | (ch << 8)); + return (char) (((ch & 0xFF00) >> 8) | (ch << 8)); } } diff --git a/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java b/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java index c10ddebd0..6d50d2c7e 100644 --- a/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java +++ b/classlib/src/main/java/org/teavm/classlib/java/lang/TShort.java @@ -133,6 +133,6 @@ public class TShort extends TNumber implements TComparable { } public static short reverseBytes(short i) { - return (short) ((i << 8) | (i >>> 8)); + return (short) ((i << 8) | ((i & 0xFF00) >> 8)); } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/CharacterTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/CharacterTest.java index 380e36b5a..63938fefa 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/CharacterTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/CharacterTest.java @@ -71,4 +71,10 @@ public class CharacterTest { assertEquals('Ü', Character.toUpperCase('ü')); assertEquals('Ü', Character.toUpperCase('Ü')); } + + @Test + public void reverseBytes() { + assertEquals((char) 12405, Character.reverseBytes((char) 30000)); + assertEquals((char) -12150, Character.reverseBytes((char) -30000)); + } } diff --git a/tests/src/test/java/org/teavm/classlib/java/lang/ShortTest.java b/tests/src/test/java/org/teavm/classlib/java/lang/ShortTest.java index 2abbad480..3a41d2121 100644 --- a/tests/src/test/java/org/teavm/classlib/java/lang/ShortTest.java +++ b/tests/src/test/java/org/teavm/classlib/java/lang/ShortTest.java @@ -50,4 +50,10 @@ public class ShortTest { public void bytes() { assertEquals(2, Short.BYTES); } + + @Test + public void reverseBytes() { + assertEquals((short) 12405, Short.reverseBytes((short) 30000)); + assertEquals((short) -12150, Short.reverseBytes((short) -30000)); + } }