George Irimiciuc
George Irimiciuc

Reputation: 4633

Getting length of a number's bits

  int g = 269;//1_0000_1101
    System.out.println( length(Integer.toBinaryString(g)));

This will print 18(why?), but I want it to print 9. How can I do that?

Upvotes: 0

Views: 103

Answers (2)

user2591612
user2591612

Reputation:

System.out.println(Integer.toBinaryString(g).length());

Would print out the length of the returned String from toBinaryString. You are measuring the length of the statement otherwise.

Upvotes: 1

Related Questions