Java中的Integer reverseBytes()方法

爪哇。整型。reverseBytes(int a)是一个内置方法,它返回通过反转 字节 在指定int值的两个补码表示中。

null

语法:

public static int reverseBytes(int a)

参数: 该方法采用一个参数 A. 整数类型,其字节将被反转。

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

例如:

Input: 75
Output: 1258291200
Explanation:
Consider an integer a = 75 
Binary Representation = 1001011
Number of one bit = 4 
After reversing the bytes we get = 1258291200

Input: -43
Output: -704643073

下面的程序演示了java。整型。reverseBytes()方法: 项目1: 对于一个正数。

// Java program to illustrate the
// Java.lang.Integer.reverseBytes() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
int a = 61 ;
System.out.println( " Integral Number = " + a);
// It will return the value obtained by reversing the bytes in the
// specified int value
System.out.println( "After reversing the bytes we get = " +
Integer.reverseBytes(a));
}
}


输出:

Integral Number = 61
After reversing the bytes we get = 1023410176

项目2: 对于负数。

// Java program to illustrate the
// Java.lang.Integer.reverseBytes() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
int a = - 43 ;
System.out.println( " Integral Number = " + a);
// It will return the value obtained by reversing the bytes in the
// specified int value
System.out.println( "After reversing the bytes we get = " +
Integer.reverseBytes(a));
}
}


输出:

Integral Number = -43
After reversing the bytes we get = -704643073

方案3: 对于十进制值和字符串。 注: 当十进制值和字符串作为参数传递时,它返回错误消息。

// Java program to illustrate the
// Java.lang.Integer.reverseBytes() method
import java.lang.*;
public class Geeks {
public static void main(String[] args)
{
int a = 37.81 ;
System.out.println( " Integral Number = " + a);
// It will return the value obtained by reversing the bytes in the
// specified int value
System.out.println( "After reversing the bytes we get = " +
Integer.reverseBytes(a));
a = "81" ; // compile time error will be generated
System.out.println( " Integral Number = " + a);
System.out.println( "After reversing the bytes we get = " +
Integer.reverseBytes(a));
}
}


输出:

prog.java:9: error: incompatible types: possible lossy conversion from double to int
    int a = 37.81;
            ^
prog.java:18: error: incompatible types: String cannot be converted to int
    a = "81";
        ^
2 errors

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享