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

Side by Side Diff: pkg/fixnum/lib/src/int32.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, 2 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/fixnum/lib/src/int64.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 part of fixnum; 5 part of fixnum;
6 6
7 /** 7 /**
8 * An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1]. 8 * An immutable 32-bit signed integer, in the range [-2^31, 2^31 - 1].
9 * Arithmetic operations may overflow in order to maintain this range. 9 * Arithmetic operations may overflow in order to maintain this range.
10 */ 10 */
(...skipping 314 matching lines...) Expand 10 before | Expand all | Expand 10 after
325 } 325 }
326 return _i >= _toInt(other); 326 return _i >= _toInt(other);
327 } 327 }
328 328
329 bool get isEven => (_i & 0x1) == 0; 329 bool get isEven => (_i & 0x1) == 0;
330 bool get isMaxValue => _i == 2147483647; 330 bool get isMaxValue => _i == 2147483647;
331 bool get isMinValue => _i == -2147483648; 331 bool get isMinValue => _i == -2147483648;
332 bool get isNegative => _i < 0; 332 bool get isNegative => _i < 0;
333 bool get isOdd => (_i & 0x1) == 1; 333 bool get isOdd => (_i & 0x1) == 1;
334 bool get isZero => _i == 0; 334 bool get isZero => _i == 0;
335 int get bitLength => _i.bitLength;
335 336
336 int get hashCode => _i; 337 int get hashCode => _i;
337 338
338 Int32 abs() => _i < 0 ? new Int32(-_i) : this; 339 Int32 abs() => _i < 0 ? new Int32(-_i) : this;
339 340
341 Int32 clamp(lowerLimit, upperLimit) {
342 if (this < lowerLimit) {
343 if (lowerLimit is IntX) return lowerLimit.toInt32();
344 if (lowerLimit is int) return new Int32(lowerLimit);
345 throw new ArgumentError(lowerLimit);
346 } else if (this > upperLimit) {
347 if (upperLimit is IntX) return upperLimit.toInt32();
348 if (upperLimit is int) return new Int32(upperLimit);
349 throw new ArgumentError(upperLimit);
350 }
351 return this;
352 }
353
340 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i); 354 int numberOfLeadingZeros() => _numberOfLeadingZeros(_i);
341 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i); 355 int numberOfTrailingZeros() => _numberOfTrailingZeros(_i);
342 356
357 Int32 toSigned(int width) {
358 if (width < 1 || width > 32) throw new ArgumentError(width);
359 return new Int32(_i.toSigned(width));
360 }
361
362 Int32 toUnsigned(int width) {
363 if (width < 0 || width > 32) throw new ArgumentError(width);
364 return new Int32(_i.toUnsigned(width));
365 }
366
343 List<int> toBytes() { 367 List<int> toBytes() {
344 List<int> result = new List<int>(4); 368 List<int> result = new List<int>(4);
345 result[0] = _i & 0xff; 369 result[0] = _i & 0xff;
346 result[1] = (_i >> 8) & 0xff; 370 result[1] = (_i >> 8) & 0xff;
347 result[2] = (_i >> 16) & 0xff; 371 result[2] = (_i >> 16) & 0xff;
348 result[3] = (_i >> 24) & 0xff; 372 result[3] = (_i >> 24) & 0xff;
349 return result; 373 return result;
350 } 374 }
351 375
376 double toDouble() => _i.toDouble();
352 int toInt() => _i; 377 int toInt() => _i;
353 Int32 toInt32() => this; 378 Int32 toInt32() => this;
354 Int64 toInt64() => new Int64(_i); 379 Int64 toInt64() => new Int64(_i);
355 380
356 String toString() => _i.toString(); 381 String toString() => _i.toString();
357 String toHexString() => _i.toRadixString(16); 382 String toHexString() => _i.toRadixString(16);
358 String toRadixString(int radix) => _i.toRadixString(radix); 383 String toRadixString(int radix) => _i.toRadixString(radix);
359 } 384 }
OLDNEW
« no previous file with comments | « no previous file | pkg/fixnum/lib/src/int64.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698