BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Integer bitCount() Method

Last Updated: October 21, 2022 by Chaitanya Singh | Filed Under: java

The bitCount() method of Java Integer class returns the number of 1’s bits in the two’s complement representation of the given integer value.

public class JavaExample {
  public static void main(String args[])
  {
    int i = 24;
    // binary equivalent of 24 is: 11000
    System.out.println(Integer.toBinaryString(i));
    System.out.println(Integer.bitCount(i));
  }
}

Output:

11000
2

Syntax of bitCount() method

public static int bitCount(int i)

bitCount() Parameters

  • i: An integer number

bitCount() Return Value

  • The bitCount() method has int return type. It returns the number of 1’s bits in the two’s complement representation of the given integer number. This process of counting 1’s in a binary number is also known as population count.

Example 1

public class JavaExample {
  public static void main(String args[])
  {
    int i = 50;
    // binary equivalent of 50 is: 110010
    System.out.println(Integer.toBinaryString(i));
    System.out.println(Integer.bitCount(i));
  }
}

Output:

Java Integer bitCount Example Output1

Example 2

import java.util.Scanner;
public class JavaExample {
  public static void main(String[] args) {
    int i;
    Scanner scan = new Scanner(System.in);
    System.out.print("Input an integer number: " );
    i = scan.nextInt();
    System.out.println("Entered number is: "+i);
    // Entered number binary conversion
    System.out.println("Binary equivalent: "+
            Integer.toBinaryString(i));
    // Print 1's count
    System.out.println("Number of 1's in input number: "+
            Integer.bitCount(i));
  }
}

Output:

Java Integer bitCount Example Output2

Example 3

public class JavaExample {
  public static void main(String args[])
  {
    int i = Integer.MAX_VALUE;
    int i2 = Integer.MIN_VALUE;
    System.out.println(Integer.toBinaryString(i));
    System.out.println(Integer.bitCount(i));
    System.out.println(Integer.toBinaryString(i2));
    System.out.println(Integer.bitCount(i2));
  }
}

Output:

Java Integer bitCount Example Output3

Top Related Articles:

  1. Java Integer byteValue() Method
  2. When to use ArrayList and LinkedList in Java
  3. Convert Comma Separated String to HashSet in Java
  4. Static and dynamic binding in java
  5. Java String to boolean Conversion with examples

Tags: Integer

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap