java short =_Java Short reverseBytes()用法及代码示例

Short类的reverseBytes()方法是Java中的内置方法,用于返回通过反转指定的short值的二进制补码表示形式的字节顺序而获得的值。

用法:

public static short reverseBytes(short a)

参数:该方法采用一个简短类型的参数a,其字节将被反转。

返回值:该方法将返回通过反转指定的short值中的字节获得的值。

例子:

Input: 75

Output: 1258291200

Explanation:

Consider an short a = 75

Binary Representation = 1001011

Number of one bit = 4

After reversing the bytes = 1258291200

Input: -43

Output: -704643073

以下示例程序旨在说明Short.reverseBytes()方法:

示例1:为正数。

// Java program to illustrate the

// Short.reverseBytes() method

import java.lang.*;

public class Geeks {

public static void main(String[] args)

{

// Create a Short instance

short a = 61;

// Print the value before reversal of Bytes

System.out.println("Original value = " + a);

// Reverse the bytes in the specified short value

// Using reverseBytes() method

System.out.println("After reversing the bytes :  \n"

+ "New value : "

+ Short.reverseBytes(a));

}

}

输出:

Original value = 61

After reversing the bytes :

New value : 15616

示例2:为负数。

// Java program to illustrate the

// Short.reverseBytes() method

import java.lang.*;

public class Geeks {

public static void main(String[] args)

{

// Create a Short instance

short a = -45;

// Print the value before reversal of Bytes

System.out.println("Original value = " + a);

// Reverse the bytes in the specified short value

// Using reverseBytes() method

System.out.println("After reversing the bytes :  \n"

+ "New value : "

+ Short.reverseBytes(a));

}

}

输出:

Original value = -45

After reversing the bytes :

New value : -11265

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值