Java.lang Package Classes

Java Integer - highestOneBit() Method



The java.lang.Integer.highestOneBit() method returns an int value with at most a single one-bit, in the position of the highest-order ("leftmost") one-bit in the specified int value. Returns zero if the specified value has no one-bits in its two's complement binary representation, that is, if it is equal to zero.

Syntax

public static int highestOneBit(int i)

Parameters

i Specify the value whose highest one bit is to be computed.

Return Value

Returns an int value with a single one-bit, in the position of the highest-order one-bit in the specified value, or zero if the specified value is itself equal to zero.

Exception

NA.

Example:

In the example below, the java.lang.Integer.highestOneBit() method returns an int value with a single one-bit, in the position of the highest-order one-bit in the specified value.

import java.lang.*;

public class MyClass {
  public static void main(String[] args) {
    
    //creating int value
    int x = 135;

    //printing x
    System.out.println("The x is = " + x);     

    //printing the binary representation of x
    System.out.println("The x in binary is = " + Integer.toBinaryString(x)); 

    //printing the highest-order one-bit
    System.out.println("Highest-order one-bit = " + Integer.highestOneBit(x)); 
  }
}

The output of the above code will be:

The x is = 135
The x in binary is = 10000111
Highest-order one-bit = 128

❮ Java.lang - Integer

5