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.
Yes, just test each bit in turn using a mask. For integers there are 32 possible bits.
Luckily java provides this for you:
If you wanted to do it yourself:
Output
If alternatively you wanted the “length”, i.e. the number of 0s or 1s…
Convert to string and find length
Use some knowledge of maths to take the base(2) log of the value.