Java Short.reverseBytes(short i) example

Java Short.reverseBytes(short i) example

By Yashwant Chavan, Views 11, Date 16-May-2018

In this java tutorial, You will learn how to use reverseBytes(short i) method of Short class. This method, Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified short value. This method returns the value obtained by reversing (or, equivalently, swapping) the bytes in the specified short value.

tags Java

Returns the value obtained by reversing the order of the bytes in the two's complement representation of the specified short value.

Syntax

public static short reverseBytes(short i)

Returns

The value obtained by reversing (or, equivalently, swapping) the bytes in the specified short value.

Java Short reverseBytes(short i) example

  • Assign two short values.
  • Create an instance of short values e.g new Short(value);
  • Short.reverseBytes(firstValue);It will return the reversing order of the bytes of firstValue.
  • Short.reverseBytes(secondValue);It will return the reversing order of the bytes of secondValue.
  • Print the values.
package com.technicalkeeda;

public class App {

    public static void main(String[] args) {

        short value1 = 50;
        Short firstValue = new Short(value1);

        short value2 = -20;
        Short secondValue = new Short(value2);

        int result1 = Short.reverseBytes(firstValue);
        int result2 = Short.reverseBytes(secondValue);

        System.out.println("The reversing order of the bytes of firstValue :- " + result1);
        System.out.println("The reversing order of the bytes of secondValue :- " + result2);
    }
}

output

The reversing order of the bytes of firstValue :- 12800
The reversing order of the bytes of secondValue :- -4865

Suggested Posts

Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]