Java Integer highestOneBit()
The highestOneBit() method of Java Integer class returns an int value with at most a single one-bit, in the position of the highest-order one-bit in the specified int value. Syntax public static int highestOneBit(int i) Parameters The parameter ‘i’ represents the value whose highest one bit is to be calculated. Return Value This method returns an int value with a single one-bit in the position of the highest-order. Example 1
public class JavaIntegerHighestOneBitExample1 { public static void main(String[] args) { Integer a=234; Integer b=67; //returns an int value with at most a single one-bit, in the position of the highest-order one-bit int val=Integer.highestOneBit(a); System.out.println("Highest one bit of "+a+" : "+val); System.out.println("Highest one bit of "+b+" : "+Integer.highestOneBit(b)); } }Output
Highest one bit of 234 : 128 Highest one bit of 67 : 64Example 2
public class JavaIntegerHighestOneBitExample2 { public static void main(String[] args) { Integer a=-0; //returns 0 if the integer value passed is zero int val=Integer.highestOneBit(a); System.out.println("Highest one bit of "+a+" : "+val); } }Output
Highest one bit of 0 : 0Example 3
public class JavaIntegerHighestOneBitExample3 { public static void main(String[] args) { Integer a=Integer.MAX_VALUE; //returns max one bit if the integer value passed is MAX_Value int val=Integer.highestOneBit(a); System.out.println("Highest one bit of "+a+" : "+val); } }Output
Highest one bit of 2147483647 : 1073741824