當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Java Integer toUnsignedLong()用法及代碼示例

toUnsignedLong() 是一個 Java Integer 類方法,它通過無符號轉換將參數轉換為 long。在對 long 的無符號轉換中,long 的 high-order 32 位為零,低 32 位等於整數參數的位。

用法:

以下是 toUnsignedLong() 方法的聲明:

public static long toUnsignedLong(int i)

參數:

數據類型 參數 描述 必需/可選
int i 它是一個用於轉換為 unsigned long 的值。 Required

返回值:

toUnsignedLong() 方法返回通過無符號轉換轉換為 long 的參數。

異常:

NA

兼容版本:

Java 1.8 及以上

例子1

public class IntegerToUnsignedLongExample1 {
    public static void main(String[] args) {
        int x1 = -5;
        int x2 = 5;
        int y1 = -6;
        int y2 = 6;
        //Performs unsigned operation treating x and y holding unsigned values
        long ux1 = Integer.toUnsignedLong(x1);
        long ux2 = Integer.toUnsignedLong(x2);
        long uy1 = Integer.toUnsignedLong(y1);	 
        long uy2 = Integer.toUnsignedLong(y2);	    
        System.out.println("Unsigned x1  = " + ux1);
        System.out.println("Unsigned x2  = " + ux2);
        System.out.println("Unsigned y1  = " + uy1);	    
        System.out.println("Unsigned y2  = " + uy2);
          }
}

輸出:

Unsigned x1  = 4294967291
Unsigned x2  = 5
Unsigned y1  = 4294967290
Unsigned y2  = 6

例子2

import java.util.Scanner;
public class IntegerToUnsignedLongExample2 {
    public static void main(String[] args) {
        //Enter user input
        System.out.print("Enter a value to perform unsigned operation:");
        Scanner s = new Scanner(System.in);
        Integer value = s.nextInt();								
        //Get the toUnsignedLong() method result
        Long result = Integer.toUnsignedLong(value);				
        // print the result
        System.out.println("Output:"+result);
        s.close();
            }
}

輸出:

Enter a value to perform unsigned operation:32435
Output:32435

例子3

import java.util.Scanner;
public class IntegerToUnsignedLongExample3 {
    public static void main(String[] args) {
        //Enter user input
        System.out.print("Input value to perform unsigned operation:");
        Scanner s = new Scanner(System.in);
        Integer value = s.nextInt();								
        //Print the result
        System.out.println("Output:"+Integer.toUnsignedLong(value));
        s.close();
            }
}

輸出:

Input value to perform unsigned operation:-GSDJD
Exception in thread "main" java.util.InputMismatchException
	at java.base/java.util.Scanner.throwFor(Scanner.java:939)
	at java.base/java.util.Scanner.next(Scanner.java:1594)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2258)
	at java.base/java.util.Scanner.nextInt(Scanner.java:2212)
	at myPackage.IntegerToUnsignedLongExample3.main(IntegerToUnsignedLongExample3.java:8)






相關用法


注:本文由純淨天空篩選整理自 Java Integer toUnsignedLong() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。