Java Integer.highestOneBit(int i) example

Java Integer.highestOneBit(int i) example

By Yashwant Chavan, Views 2, Date 21-Apr-2018

In this java tutorial, You will learn how to use highestOneBit(int i) method of class Integer. This 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.

tags Java

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.

Syntax

public static int highestOneBit(int i)

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.

Java Integer highestOneBit(int i) example

  • Assign two integer values.
  • Integer.highestOneBit(firstValue);It will return the highest one bit of firstValue.
  • Integer.highestOneBit(secondValue);It will return the highest one bit of secondValue.
  • Print the values.
package com.technicalkeeda;

public class App {

    public static void main(String[] args) {

        Integer firstValue = 125;
        Integer secondValue = 42;

        int result1 = Integer.highestOneBit(firstValue);
        int result2 = Integer.highestOneBit(secondValue);

        System.out.println("Highest one bit of firstValue :- " + result1);
        System.out.println("Highest one bit of secondValue :- " + result2);
    }
}

output

Highest one bit of firstValue :- 64
Highest one bit of secondValue :- 32
Yashwant

Yashwant

Hi there! I am founder of technicalkeeda.com and programming enthusiast. My skills includes Java,J2EE, Spring Framework, Nodejs, PHP and lot more. If you have any idea that you would want me to develop? Lets connect: yashwantchavan[at][gmail.com]