28

For an app, I need a 64bit unsigned int. Looking at dart documentation I did not see how to exactly go about declaring one.

Can anyone tell me how this is done? I will use this "64bit unsigned int" in bitwise operation.

Ragas
  • 3,005
  • 6
  • 25
  • 42

2 Answers2

27

Dart does not have a native unsigned 64-bit integer.

For many operations, you can just use the signed 64-bit integer that an int is, and interpret it as unsigned. It's the same bits. That won't work with division, though. (And if it's for the web, then an int is a JavaScript number, and you need to do something completely different).

The simplest general approach is to use a BigInt and use toUnsigned(64) after you do any operations on it.

lrn
  • 64,680
  • 7
  • 105
  • 121
  • my understanding is that in 64 bit signed `int` one of the bits is reserved for a sign, and only 63 bits are available to store value. I need all 64 bits to be used for value. – Ragas Dec 04 '18 at 11:31
  • 1
    My point, which I might not be making very well, is that the *bits* are the same. A signed 64-bit integer does not reserve a bit for the sign, it is a 2's complement representation where having the most significant bit set makes the number negative, but modulo 2^64 it's still the same number as the unsigned interpretation of the same bits. For many operations, the result is independent of whether those 64 bits are interpreted as signed or unsigned. It will matter in the end, if you want to print the result, or if you want to divide or do right shifts. If you need that, use `BigInt`. – lrn Dec 05 '18 at 10:20
  • Thanks, looks like I have to use `BigInt` as I need to use shifts. – Ragas Dec 05 '18 at 15:41
  • I agree. When Dart gets the `>>>` operator back, you should be able to handle unsigned shifts too, but it's not there yet. – lrn Dec 05 '18 at 15:46
  • Even for short, we have to use int? – Uday Jul 14 '20 at 13:12
  • Dart does not allow you access to the memory layout. There is only one fixed-size native integer type in Dart. It's 64-bit signed, which also matches the size of pointers in modern CPUs, so that keeps everything the same size. There is no "short". If you want to store 16-bit values in 16 bits, you can store them in an `Int16List`, but they will become `int` values when read. – lrn Jul 14 '20 at 14:00
4

Just use fixnum

You can easily create an int64 with Int64()

Franci
  • 2,157
  • 1
  • 21
  • 33