2

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.

4
  • In Java, int has size 4 bytes = 32 bits. Bam, there you go! As a more serious answer, if you have them stored as integers already, you can use a loop and bit-shifting, keeping track of the last 1 you find, because what you're actually looking for is the minimum number of bits to represent that number, right? Commented Mar 12, 2012 at 23:46
  • Then look at Adam's answer below, and modify it. It's not exactly the code you want, it only count's the number of 1's, and you need the number of 0's and 1's, up to the point where the last 1 is reached (you'll read the whole thing, but you'll want to discard the segment from the last 1 to the end. Commented Mar 12, 2012 at 23:55
  • @prelic, is it possible to return Int back to bit representation and stored in String and find length. Commented Mar 12, 2012 at 23:55
  • 1
    Yes it is: Integer.toBinaryString(int i) Commented Mar 12, 2012 at 23:57

1 Answer 1

5

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))); 
Sign up to request clarification or add additional context in comments.

5 Comments

Are you sure he wants only the number of 1 bits? Why not just the minimum number of bits needed to represent the number?
@prelic You're right, the question is not clear. I've added alternative solutions to count 0s and 1s.
@Adam - You're right, it just as easily could have been yours, I just thought the test case 00 was weird. Now the question is, does your code output 32 for all inputs because of the whole int being 4 bytes in java? Or does it actually cut off the leading 0s? Either way, now I like your answer. +1
The manual bit count works correctly, so does the string solution. The math solution fails with NaN as 0xffffffff when converted to a double is negative because of the leftmost sign-bit.
@prelic Fixed the maths solution by "unsigning" the int into a double.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.