in reply to Re: Decimal to Hexadecimal conversion and extraction MSB
in thread Decimal to Hexadecimal conversion and extraction MSB

my $msb = $decimal_number & 0x8000;
extracts the leftmost bit of a 16 bit integer, not the leftmost '1' bit of any length integer, which is what was asked (I think).

CountZero

A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Replies are listed 'Best First'.
Re^3: Decimal to Hexadecimal conversion and extraction MSB
by eye (Chaplain) on Oct 17, 2009 at 09:09 UTC
    I concur that the OP seems to be asking something different from what I answered. But the leftmost bit of a non-negative integer of any length is one unless the integer is zero. So I don't think that is what was intended.

    The OP could also have been asking for the position of the leftmost "1" bit. If the integer is not zero, this is the integer portion of the quotient "log(number) / log(2)" (for a non-zero base log).

    I read the tea leaves and inferred that the OP wanted to work with integers of a fixed size. (The part of the question that asked about bit shifting gave me a strong push in this direction. That is surely a prejudice based on my experience.)