Search code examples
javabinaryhex

Determining bits of non-binary represented numbers


I'm reading "Thinking in Java", and there I read:

"Start with a number that has a binary one in the most significant position (hint: Use a hexadecimal constant)."

How can I know (without Google and without trying to represent some hex constants in binary system) which hexadecimal constant have which bits? Either it's most significant or some other bits.

The book assumes that for this exercise I should try out hex constants before I find one whose most significant bit is 1?

Or there is some shortcut?

I tried to think of a shortcut/way of knowing how to figure out what value has most significant bit of a hex constant...


Solution

  • For integral types, you will know the most significant bit is set when:

    1. The number is negative
    2. The number exceeds the MAX_VALUE attribute of the object wrapper class of your primitive type

    For the purposes of the above, I exclude char since its function is not normally one of manipulating numbers in a normal way