Permalink
Browse files

explain what lsb and msb do for 0 and negatives

  • Loading branch information...
TimToady committed May 29, 2013
1 parent 617f456 commit 949c62adefef7905efe2cf6d41de09e68ec0d77e
Showing with 17 additions and 0 deletions.
  1. +17 −0 S32-setting-library/Numeric.pod
@@ -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

1 comment on commit 949c62a

@leto

This comment has been minimized.

Show comment Hide comment
@leto

leto May 29, 2013

Member

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

Member

leto commented on 949c62a May 29, 2013

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

Please sign in to comment.