Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7068857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:22:30+00:00 2026-05-28T05:22:30+00:00

I have an assignment in which I need to create a function that tells

  • 0

I have an assignment in which I need to create a function that tells you how many 1’s are in the binary notation of a integer. I did this already by creating my own algorithm. The second step is to use java.math.BigInteger.bitCount() to accomplish the same thing. I looked this up in the Java API but can someone put this into English and explain how it’s relevant to finding the number of 1’s in the binary notation of an integer, and perhaps also an example. I tried googling but found nothing but the following definition.

public int bitCount()

Description:
Returns the number of bits in the two’s complement representation of this number that differ from its sign bit. This method is useful when implementing bit-vector style sets atop BigIntegers.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:22:30+00:00Added an answer on May 28, 2026 at 5:22 am
    jcomeau@intrepid:/tmp$ cat test.java; javac test.java; java test
    import java.math.BigInteger;
    public class test {
     public static void main(String[] args) {
      System.out.println("one bits: " + new BigInteger("0f0f0f0f0f0f0", 16).bitCount());
     }
    }
    one bits: 24
    

    Modified code for new comment:

    jcomeau@intrepid:/tmp$ cat test.java; javac test.java; java test 0xf0f0f0f0f0f0 0x200 200 1 0
    import java.math.BigInteger;
    public class test {
     public static void main(String[] args) {
      BigInteger number = null;
      for (String arg : args) {
       if (arg.startsWith("0x")) {
        number = new BigInteger(arg.substring(2), 16);
       } else {
        number = new BigInteger(arg);  // decimal by default
       }
       System.out.println("one bits in " + arg + ": " + number.bitCount());
      }
     }
    }
    one bits in 0xf0f0f0f0f0f0: 24
    one bits in 0x200: 1
    one bits in 200: 3
    one bits in 1: 1
    one bits in 0: 0
    

    Obviously, from reading the javadoc you posted, this may or may not conform to what you or your professor expect for negative numbers. This will return 0 “1” bits for negative 1, for example, since all the bits are the same as the sign bit. But it should work for all positive values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this assignment due that requires the usage of FLTK. The code is
I have a database assignment which I have to create some relational algebra for
I have a school assignment where I need to create a data-driven style of
As part of an assignment I need to create a shipping program that checks
I have a homework assignment that asks to create an order form. The order
I've this question from an assignment to create a Store which rent out books,
So I am being taught assembly and we have an assignment which is to
Are there exceptions for types which can't have thier assignment operator overloaded? Specifically, I'm
I have my first professional assignment of making a website in which a photographer's
I have an assignment to create a GUI using MATLAB GUIDE and am having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.