java.lang.Integer.reverse()是Java中的內置方法,用於返回指定int值的二進製補碼二進製表示形式的位的相反順序。
用法:
public static int reverse(int a)
參數:參數a是一個整數值,其位將被取反。
返回值:該方法返回通過反轉指定int值中的位順序獲得的值。
例子:
Input: 86 Output: 1778384896 Explanation: Consider an integer a = 86 Binary Representation = 1010110 The number of one bit = 4 After reversing it is = 1778384896 Input: 168 Output: 352321536
以下程序說明了java.lang.Integer.reverse()方法:
程序1:為正數。
// Java program to illustrate the
// Java.lang.Integer.reverse() method
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
int a = 168;
System.out.println("Number = " + a);
// It returns the value obtained by reversing order of the bits
System.out.println("By reversing we get = " + Integer.reverse(a));
}
}
輸出:
Number = 168 By reversing we get = 352321536
程序2:對於負數。
// Java program to illustrate the
// Java.lang.Integer.reverse() method
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
int a = -72;
System.out.println("Number = " + a);
// It returns the value obtained by reversing order of the bits
System.out.println("By reversing we get = " + Integer.reverse(a));
}
}
輸出:
Number = -72 By reversing we get = 503316479
程序3:用於十進製值和字符串。
注意:當將十進製值或字符串作為參數傳遞時,它將引發錯誤消息。
// Java program to illustrate the
// Java.lang.Integer.reverse() method
import java.lang.*;
public class geeks {
public static void main(String[] args)
{
int a = 37.9;
System.out.println("Number = " + a);
// It returns the value obtained by reversing order of the bits in
// the specified int value
System.out.println("By reversing we get = " + Integer.reverse(a));
a = "21";
System.out.println("Number = " + a);
// It returns the value obtained by reversing order of the bits in
// the specified int value
System.out.println("By reversing we get = " + Integer.reverse(a));
}
}
輸出:
prog.java:9: error: incompatible types: possible lossy conversion from double to int int a = 37.9; ^ prog.java:17: error: incompatible types: String cannot be converted to int a = "21"; ^ 2 errors
相關用法
- Java Java lang.Long.reverse()用法及代碼示例
- Java Integer sum()用法及代碼示例
- Java Integer decode()用法及代碼示例
- Java Integer valueOf()用法及代碼示例
- Java Integer toOctalString()用法及代碼示例
- Java Integer floatValue()用法及代碼示例
- Java Integer hashCode()用法及代碼示例
- Java Integer rotateLeft()用法及代碼示例
- Java Integer intValue()用法及代碼示例
- Java Integer compareTo()用法及代碼示例
- Java Integer highestOneBit()用法及代碼示例
- Java Integer bitCount()用法及代碼示例
- Java Integer reverseBytes()用法及代碼示例
- Java Integer compareUnsigned()用法及代碼示例
- Java Integer signum()用法及代碼示例
注:本文由純淨天空篩選整理自ankita_chowrasia大神的英文原創作品 Integer reverse() Method In Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。