Get length of bits used in int

Posted by sigvardsen on Stack Overflow See other posts from Stack Overflow or by sigvardsen
Published on 2010-05-23T13:39:59Z Indexed on 2010/05/23 13:50 UTC
Read the original article Hit count: 304

Filed under:
|
|
|

If you have the binary number 10110 how can I get it to return 11111? e.g a new binary number that sets all bits to 1 after the first 1, there are some likewise examples listed below:

101 should return 111 (3 bit length) 011 should return 11 (2 bit length) 11100 should be return 11111 (5 bit length) 101010101 should return 111111111 (9 bit length)

How can this be obtained the easiest way in Java? I could come up with some methods but they are not very "pretty".

© Stack Overflow or respective owner

Related posts about java

Related posts about binary