Integer highestOneBit() Method in Java
The Integer.highestOneBit() method of java.lang returns an integer value with at most a single one-bit which is in the position of the highest-order (ie.leftmost) one-bit for the given int value. If the specified value has no one-bits in its two’s complement binary representation then it returns zero, simply if it is equal to zero.
Syntax:
public static int highestOneBit(int a)
Parameters: The method takes one parameter a of integer type that refers to the value whose highest order bit is to be returned or on which the operation is to be performed.
Returns : The method can return two types of values:
- Returns an integer value with a single 1-bit, in the position of the highest-order one-bit in the specified value
- Returns zero if the specified value is equal to zero.
Examples:
Input: 19 Output: Highest one bit of the given integer is = 16 Input: -9 Output: Highest one bit of the given integer is = -2147483648 Explanation: Consider any integer a = 19 Binary Representation = 0001 0011 Highest bit(at 4) i.e.0001 0000 so result = 2^4=16
Below programs illustrate the Java.lang.Integer.highestOneBit() Method.
Program 1: For a positive number.
// Java praogram to illustrate the // Java.lang.Integer.highestOneBit() Method import java.lang.*; public class HighestOneBit { public static void main(String[] args) { int a = 29; System.out.println("Number is = " + a); // returns an int value with at most a single one-bit, in the position // of the highest-order or the leftmost one-bit in the specified int value System.out.print("Highest one bit of the given integer is = "); System.out.println(Integer.highestOneBit(a)); a = 44; System.out.println("Number is = " + a); System.out.print("Highest one bit of the given integer is = "); System.out.println(Integer.highestOneBit(a)); } } |
Number is = 29 Highest one bit of the given integer is = 16 Number is = 44 Highest one bit of the given integer is = 32
Program 2: For a negative number.
// Java praogram to illustrate the // Java.lang.Integer.highestOneBit() Method import java.lang.*; public class HighestOneBit { public static void main(String[] args) { int a = -9; System.out.println("Number is = " + a); // returns an int value with at most a single one-bit, in the position // of the highest-order or the leftmost one-bit in the specified int value System.out.print("Highest one bit of the given integer is = "); System.out.println(Integer.highestOneBit(a)); } } |
Number is = -9 Highest one bit of the given integer is = -2147483648
Program 3: For a decimal value.
Note:It returns an error message when a decimal value is passed as an argument because of incompatible types.
// Java praogram to illustrate the // Java.lang.Integer.highestOneBit() Method import java.lang.*; public class HighestOneBit { public static void main(String[] args) { int a = 84.22; System.out.println("Number is = " + a); // decimal value 84.22 is passed here System.out.print("Highest one bit of the given integer is = "); System.out.println(Integer.highestOneBit(a)); } } |
prog.java:9: error: incompatible types: possible lossy conversion from double to int
int a = 84.22;
^
1 error
Program 4: For a String.
Note:It returns an error message when a String is passed as an argument because of incompatible types.
// Java praogram to illustrate the // Java.lang.Integer.highestOneBit() Method import java.lang.*; public class HighestOneBit { public static void main(String[] args) { int a = "34"; System.out.println("Number is = " + a); // decimal value 84.22 is passed here System.out.print("Highest one bit of the given integer is = "); System.out.println(Integer.highestOneBit(a)); } } |
prog.java:9: error: incompatible types: String cannot be converted to int
int a = "34";
^
1 error
Recommended Posts:
- Java lang.Long.highestOneBit() method in Java with Examples
- Integer sum() Method in Java
- Java Integer byteValue() method
- Integer decode() Method in Java
- Integer hashCode() Method in Java
- Integer reverse() Method In Java
- Java Integer bitCount() method
- Integer.numberOfTrailingZeros() Method in Java with Example
- Integer.numberOfLeadingZeros() Method in Java With Example
- Java Integer compareTo() method
- Java Integer compare() method
- Java Integer compareUnsigned() method
- Integer lowestOneBit() Method in Java
- Integer intValue() Method in Java
- Integer toOctalString() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



