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

Unified Diff: runtime/lib/integers.dart

Issue 23645003: Esoteric bit operations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: 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 | « runtime/lib/integers.cc ('k') | runtime/vm/assembler_ia32.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/lib/integers.dart
diff --git a/runtime/lib/integers.dart b/runtime/lib/integers.dart
index dca7ef8434670a2934ac7a1de4e2777691e00248..7c242ef95659a9872e9810492060f0d695f8dc13 100644
--- a/runtime/lib/integers.dart
+++ b/runtime/lib/integers.dart
@@ -95,6 +95,20 @@ class _IntegerImplementation {
bool get isNegative => this < 0;
bool get isInfinite => false;
+ int toUnsigned(int width) {
+ return this & ((1 << width) - 1);
+ }
+
+ int toSigned(int width) {
+ // The value of binary number weights each bit by a power of two. The
+ // twos-complement value weights the sign bit negatively. We compute the
+ // value of the negative weighting by isolating the sign bit with the
+ // correct power of two weighting and subtracting it from the value of the
+ // lower bits.
+ int signMask = 1 << (width - 1);
+ return (this & (signMask - 1)) - (this & signMask);
+ }
+
int compareTo(num other) {
final int EQUAL = 0, LESS = -1, GREATER = 1;
if (other is double) {
@@ -212,6 +226,8 @@ class _Smi extends _IntegerImplementation implements int {
return this;
}
int operator ~() native "Smi_bitNegate";
+ int get bitLength native "Smi_bitLength";
+
int _shrFromInt(int other) native "Smi_shrFromInt";
int _shlFromInt(int other) native "Smi_shlFromInt";
@@ -252,6 +268,7 @@ class _Mint extends _IntegerImplementation implements int {
return this;
}
int operator ~() native "Mint_bitNegate";
+ int get bitLength native "Mint_bitLength";
// Shift by mint exceeds range that can be handled by the VM.
int _shrFromInt(int other) {
@@ -275,6 +292,7 @@ class _Bigint extends _IntegerImplementation implements int {
return this;
}
int operator ~() native "Bigint_bitNegate";
+ int get bitLength native "Bigint_bitLength";
// Shift by bigint exceeds range that can be handled by the VM.
int _shrFromInt(int other) {
« no previous file with comments | « runtime/lib/integers.cc ('k') | runtime/vm/assembler_ia32.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698