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 7631257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:18:30+00:00 2026-05-31T06:18:30+00:00

I have incoming bits like, 0, 1, 11, 10 etc. Which I store in

  • 0

I have incoming bits like, 0, 1, 11, 10 etc. Which I store in a string. Then I convert the string to an Int.

Now, suppose Int A = “011” and Int B = “00”. Is it possible in java to know, how many bits was there in the string which I have converted to the Int. Thanks.

  • 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-31T06:18:32+00:00Added an answer on May 31, 2026 at 6:18 am

    Yes, just test each bit in turn using a mask. For integers there are 32 possible bits.

    Luckily java provides this for you:

    Integer.bitCount(value)
    

    If you wanted to do it yourself:

    int value = Integer.parseInt("1000101010", 2);
    
    int bitCounter = 0;
    for (int i = 0; i < Integer.SIZE; i++) {
        if (((1 << i) & value) > 0) {
            bitCounter++;
        }
    }
    System.out.println(value + " has " + bitCounter + " bits");
    

    Output

    554 has 4 bits
    

    If alternatively you wanted the “length”, i.e. the number of 0s or 1s…

    Convert to string and find length

    System.out.println(Integer.toString(value, 2).length());
    

    Use some knowledge of maths to take the base(2) log of the value.

    double valueUnsigned;
    if (value < 0) {
        valueUnsigned = (value & 0x7FFFFFF) + 0x80000000l;
    } else {
        valueUnsigned = value;
    }
    System.out.println("Maths solution " + Math.floor(1d + Math.log(valueUnsigned) / Math.log(2))); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to convert an incoming String field into a BigDecimal field that would
I have some code which is doing this: String transactionId = incoming.getJMSCorrelationID(); when I
I have unpredictable frequency of Incoming csv data file which I need to store
I have incoming data, which I store in a variable messages : connection =
I'm writing a chat and i would like to have the incoming messages on
I have a service which listens for incoming connections on a TCP\IP port number
I have a dictionary which I need to keep updated with incoming data, after
I have a webservice(.net) that will fetch(AfterReceiveRequest) the incoming message and then check the
Possible Duplicate: How can I decode html characters in c#? I have characters incoming
I have an incoming file with data as <root><![CDATA[<defs><elements> <element><item>aa</item><int>1</int></element> <element><item>bb</item><int>2</int></element> <element><item>cc</item><int>3</int></element> </elements></defs>]]></root> writing

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.