classlib: fix reverse bytes methods in primitive wrappers (#753)

This commit is contained in:
Ivan Hetman 2023-09-21 14:58:03 +03:00 committed by GitHub
parent ba7dcd13b5
commit e13746a650
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 14 additions and 2 deletions

View File

@ -734,6 +734,6 @@ public class TCharacter extends TObject implements TComparable<TCharacter> {
} }
public static char reverseBytes(char ch) { public static char reverseBytes(char ch) {
return (char) ((ch >> 8) | (ch << 8)); return (char) (((ch & 0xFF00) >> 8) | (ch << 8));
} }
} }

View File

@ -133,6 +133,6 @@ public class TShort extends TNumber implements TComparable<TShort> {
} }
public static short reverseBytes(short i) { public static short reverseBytes(short i) {
return (short) ((i << 8) | (i >>> 8)); return (short) ((i << 8) | ((i & 0xFF00) >> 8));
} }
} }

View File

@ -71,4 +71,10 @@ public class CharacterTest {
assertEquals('Ü', Character.toUpperCase('ü')); assertEquals('Ü', Character.toUpperCase('ü'));
assertEquals('Ü', Character.toUpperCase('Ü')); assertEquals('Ü', Character.toUpperCase('Ü'));
} }
@Test
public void reverseBytes() {
assertEquals((char) 12405, Character.reverseBytes((char) 30000));
assertEquals((char) -12150, Character.reverseBytes((char) -30000));
}
} }

View File

@ -50,4 +50,10 @@ public class ShortTest {
public void bytes() { public void bytes() {
assertEquals(2, Short.BYTES); assertEquals(2, Short.BYTES);
} }
@Test
public void reverseBytes() {
assertEquals((short) 12405, Short.reverseBytes((short) 30000));
assertEquals((short) -12150, Short.reverseBytes((short) -30000));
}
} }