Java Integer lowestOneBit()方法

来源:这里教程网 时间:2026-02-17 20:51:14 作者:

Java Integer lowestOneBit()方法

java.lang中的Integer.lowersOneBit()方法是一个内置的函数,用于返回一个最多只有一个比特的int值,其位置是指定int值中最低阶(即最右边)的一个比特。如果指定的值在其二补二进制表示中没有一比特,即如果数字的二进制表示等于零,该方法将返回0。
语法

public static int lowestOneBit( _int a_ )

参数: 该方法需要一个整数类型的参数,指的是要返回的最低阶位的值或要对其进行的操作。
返回: 该方法可以返回两种类型的值。

返回一个带有单个1位的整数值,位于指定值中最低阶1位的位置。如果指定的值等于零,则返回零。

例子

Input: 157Output: Lowest one bit = 1Input: 0Output: Lowest one bit = 0**Explanation:**Consider any integer a = 10Binary Representation = 0000 1010Lowest bit(at 1) i.e.0000 0010so result = 2^1=2

下面的程序说明了java.lang.Integer.lowestOneBit()方法:
程序1: 对于一个正数。

// Java program to illustrate the// java.lang.Integer.lowestOneBit() methodimport java.lang.*; public class Geeks {     public static void main(String[] args)    {         int a = 157;        System.out.println("Given Number = " + a);         // Returns an int value with at most a single one-bit        System.out.print("Lowest one bit = ");        System.out.println(Integer.lowestOneBit(a));         a = 64;        System.out.println("Given Number = " + a);         System.out.print("Lowest one bit = ");        System.out.println(Integer.lowestOneBit(a));         // Here it will return 0 since the number is itself zero        a = 0;        System.out.println("Given Number = " + a);         System.out.print("Lowest one bit = ");        System.out.println(Integer.lowestOneBit(a));    }}

输出

Given Number = 157Lowest one bit = 1Given Number = 64Lowest one bit = 64Given Number = 0Lowest one bit = 0

程序2: 对于一个负数。

// Java program to illustrate the// java.lang.Integer.lowestOneBit() methodimport java.lang.*; public class Geeks {     public static void main(String[] args)    {         int a = -157;        System.out.println("Given Number = " + a);         // It will return an int value with at most a single one-bit        System.out.print("Lowest one bit = ");        System.out.println(Integer.lowestOneBit(a));        a = -17;        System.out.println("Given Number = " + a);         System.out.print("Lowest one bit = ");        System.out.println(Integer.lowestOneBit(a));    }}

输出

Given Number = -157Lowest one bit = 1Given Number = -17Lowest one bit = 1

程序3: 对于一个十进制值和String。
注意: 当一个十进制值和一个字符串作为参数传递时,它返回一个编译时的错误信息。

// Java program to illustrate the// java.lang.Integer.lowestOneBit() methodimport java.lang.*; public class Geeks {     public static void main(String[] args)    {         // Decimal value is given        int a = 71.57;        System.out.println("Given Number = " + a);         System.out.print("Lowest one bit = ");        System.out.println(Integer.lowestOneBit(a));         // String is passed        a = "12";        System.out.println("Given Number = " + a);         System.out.print("Lowest one bit = ");        System.out.println(Integer.lowestOneBit(a));    }}

输出

prog.java:10: error: incompatible types: possible lossy conversion from double to int    int a = 71.57;            ^prog.java:17: error: incompatible types: String cannot be converted to int    a = "12";        ^2 errors

相关推荐