Code Monkey home page Code Monkey logo

integer.dart's Introduction

integer

Dart's native integers in custom bitwidth integer format. There are getters for int, to get the appropriate datatypes you need.

i2, i4, i8, i16, i32 and i64 are the standard signed integer datatypes and u1, u2, u4, u8, u16, u32, u63 are the standard unsigned integer datatypes exposed by this library. If you want to use a custom bit-width integer format, you can use the ix and ux datatypes.

All operations and functions can be seamlessly used with other integer types, and native int, double datatypes. Remember that all integer datatypes provided do not actually allocate different bit-widths in memory. This is due to the limitation of Dart's VM.

/// Use the constructor
i32(32423445435)
/// or use getters
32423445435.i_32 // -1936292933

Remember to enclose negative integer literals like -34644 or -89 in parenthesis to tell Dart that the getters should be called for the entire literal. Like this

(-98723447).u_16 // 39305

Operations

All operations are compatible with native int and are also compatible with all signed and unsigned integer datatypes.

u16 a = (-982342623).u_16;
print('Value of 16-bit unsigned integer a is $a');
print('Addition int: ${a + 862} double: ${a + 862.343}');
print('Subtraction int: ${a - 36} double: ${a - 36.909}');
print('Multiplication int: ${a * 6} double: ${a * 6.2345}');
print('Division int: ${a / 6} double: ${a / 6.2345}');
print('Euclidean Modulo int: ${a % 8} double: ${a % 8.923}');

Bitshift operations only support integer and int values

print('Left Shift: ${a << 3}');
print('Right Shift: ${a >> 9}');
print('Triple Shift: ${a >>> 5}');

If double values are used in operations of integer variables, the value is truncated after the operation.

Custom Bit-Width

Provide a custom bit-width to convert the integer to that format using ix and ux datatypes

/// 22-bit Signed Integer
(-4567233343).iX(22) // 363713

/// 48-bit Unsigned Integer
(-4567233343).uX(48) // 281470409477313

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.