当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。