Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Unified Diff: pkg/fixnum/lib/src/intx.dart

Issue 24388004: Added bitLength, clamp(), toDouble(), toSigned(), toUnsigned(). (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Use string literals in place of doubles where precision is exceeded Created 7 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/fixnum/lib/src/int64.dart ('k') | pkg/fixnum/test/int_32_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/fixnum/lib/src/intx.dart
diff --git a/pkg/fixnum/lib/src/intx.dart b/pkg/fixnum/lib/src/intx.dart
index a0cd1023f6ff204babee426315c9fe5cd2f710b4..859cb957b60510576ec649087f675796a739cfee 100644
--- a/pkg/fixnum/lib/src/intx.dart
+++ b/pkg/fixnum/lib/src/intx.dart
@@ -129,6 +129,21 @@ abstract class IntX implements Comparable {
/** Returns the absolute value of this integer. */
IntX abs();
+ /** Clamps this integer to be in the range [lowerLimit] - [upperLimit]. */
+ IntX clamp(IntX lowerLimit, IntX upperLimit);
+
+ /**
+ * Returns the minimum number of bits required to store this integer.
+ *
+ * The number of bits excludes the sign bit, which gives the natural length
+ * for non-negative (unsigned) values. Negative values are complemented to
+ * return the bit position of the first bit that differs from the sign bit.
+ *
+ * To find the the number of bits needed to store the value as a signed value,
+ * add one, i.e. use `x.bitLength + 1`.
+ */
+ int get bitLength;
+
/**
* Returns the number of high-order zeros in this integer's bit
* representation.
@@ -141,6 +156,33 @@ abstract class IntX implements Comparable {
int numberOfTrailingZeros();
/**
+ * Returns the least significant [width] bits of this integer, extending the
+ * highest retained bit to the sign. This is the same as truncating the value
+ * to fit in [width] bits using an signed 2-s complement representation. The
+ * returned value has the same bit value in all positions higher than [width].
+ *
+ * If the input value fits in [width] bits without truncation, the result is
+ * the same as the input. The minimum width needed to avoid truncation of `x`
+ * is `x.bitLength + 1`, i.e.
+ *
+ * x == x.toSigned(x.bitLength + 1);
+ */
+ IntX toSigned(int width);
+
+ /**
+ * Returns the least significant [width] bits of this integer as a
+ * non-negative number (i.e. unsigned representation). The returned value has
+ * zeros in all bit positions higher than [width].
+ *
+ * If the input fits in [width] bits without truncation, the result is the
+ * same as the input. The minimum width needed to avoid truncation of `x` is
+ * given by `x.bitLength`, i.e.
+ *
+ * x == x.toUnsigned(x.bitLength);
+ */
+ IntX toUnsigned(int width);
+
+ /**
* Returns a byte-sequence representation of this integer.
*
* Returns a list of int, starting with the least significant byte.
@@ -148,6 +190,14 @@ abstract class IntX implements Comparable {
List<int> toBytes();
/**
+ * Returns the double representation of this integer.
+ *
+ * On some platforms, inputs with large absolute values (i.e., > 2^52) may
+ * lose some of their low-order bits.
+ */
+ double toDouble();
+
+ /**
* Returns the int representation of this integer.
*
* On some platforms, inputs with large absolute values (i.e., > 2^52) may
« no previous file with comments | « pkg/fixnum/lib/src/int64.dart ('k') | pkg/fixnum/test/int_32_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698