Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Decimal to Hexadecimal conversion and extraction MSB

by eye (Chaplain)
on Oct 17, 2009 at 06:08 UTC ( #801709=note: print w/replies, xml ) Need Help??

in reply to Decimal to Hexadecimal conversion and extraction MSB

...I need to extract the left most "1" bit...

I think it is easier to work from $decimal_number. To do this, you need to specify the size of the number. As an example, I'll use 16 bit integers. You can extract the most significant bit using the bit-and operator (&) and an appropriately chosen bit mask. The bit mask needs the high bit set and all others clear. For 16 bit integers, this would be 0x8000.

my $msb = $decimal_number & 0x8000;

$msb will be 0 or 2^15.

...left shift the original hexadecimal number...

Again, start with $decimal_number. Use a bit mask to remove the most significant bit and then multiply the number by 2.

my $new_number = $decimal_number & 0x7fff; $new_number *= 2;

You can choose any intermediate result to translate to binary for display, but it is better to do this kind of manipulation on numbers rather than strings.

Caveats: The integer size you are working with must not be larger than the integer size on your system. You need to understand how your system represents integers. Usually, the most significant bit is used for the sign in integers.

Replies are listed 'Best First'.
Re^2: Decimal to Hexadecimal conversion and extraction MSB
by CountZero (Bishop) on Oct 17, 2009 at 07:08 UTC
    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).


    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

      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.)

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://801709]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2023-09-28 00:59 GMT
Find Nodes?
    Voting Booth?

    No recent polls found