How do I declare 64bit unsigned int in dart/flutter?
Asked Answered
G

2

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.

Glede answered 3/12, 2018 at 7:35 Comment(0)
S
29

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.

Seizure answered 3/12, 2018 at 9:49 Comment(6)
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.Glede
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.Seizure
Thanks, looks like I have to use BigInt as I need to use shifts.Glede
I agree. When Dart gets the >>> operator back, you should be able to handle unsigned shifts too, but it's not there yet.Seizure
Even for short, we have to use int?Boarding
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.Seizure
C
4

Just use fixnum

You can easily create an int64 with Int64()

Cutcherry answered 18/5, 2021 at 6:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.