Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
explain what lsb and msb do for 0 and negatives
  • Loading branch information
TimToady committed May 29, 2013
1 parent 617f456 commit 949c62a
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions S32-setting-library/Numeric.pod
Expand Up @@ -500,13 +500,30 @@ Returns the least significant bit position containing a 1 bit, counting
bit positions from least significant to most significant. (In other
words, it's the base 2 logarithm of number represented by that 1 bit.)

This function returns C<Nil> on a 0 value, since there are no bits set.
Negative integers are treated as 2's complement, so always have
a lowest bit set somewhere, if only the sign bit. Hence, a -32768
returns an lsb of 15 regardless of whether it's stored in an C<int16>
or an C<Int>.

=item msb

multi method msb ( Int $x: ) is export

Returns the most significant bit position containing a 1 bit, that is,
the base 2 logarithm of the top 1 bit.

This function returns C<Nil> on a 0 value. For negative values, the
function is dependent on the type. For native types, signed integers
are treated as unsigned, so a negative number stored in C<int64> will
always return 63. Negative integers stored in an C<Int> notionally
have an infinite number of 1 bits on top, which is a problem.
Instead of returning C<+Inf>, which is relatively useless, we return
the position of the first of that infinite supply of sign bits.
So C<msb(-1)> returns 0, C<msb(-2)> returns 1, and C<msb(-32768)>
returns 15, just as if we'd converted it from C<int16> to C<uint16>
and examined that for its top bit.

=back

=head2 Rat
Expand Down

1 comment on commit 949c62a

@leto
Copy link

@leto leto commented on 949c62a May 29, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the behavior of these functions on the inputs of NaN, Inf and -Inf part of the spec?

Please sign in to comment.