Warning: Parameter 1 to Language::getMagic() expected to be a reference, value given in /home/wikija5/public_html/w/includes/StubObject.php on line 58

Warning: Parameter 3 to renderSEO() expected to be a reference, value given in /home/wikija5/public_html/w/includes/parser/Parser.php on line 3243
Byte to short conversion - WikiJava
Saturday, 28th March 2015
Follow WikiJava on twitter now. @Wikijava

Byte to short conversion

From WikiJava

Jump to: navigation, search
The author suggests:

buy this book

Sometimes it's necessary to convert from bytes to shorts. Have you ever tried to store a video coordinate? The shorts are the solutions.


the code

 * @param left
 *            The most valued byte.
 * @param right
 *            The less valued byte.
 * @return A short mixing the two parts.
private static short byte2short(final byte left, final byte right) {
	return (short) ((left & 0xff) << 8 | right & 0xff);

As you can see we move the left one to the left, and "add" using the OR (|) operator with the right one. The bitmask 0xff is necessary to "hide" the other part of the number.

Any comment is appreciated.

Comments from the users

To be notified via mail on the updates of this discussion you can login and click on watch at the top of the page

Comments on wikijava are disabled now, cause excessive spam.