matanlurey / binary.dart Goto Github PK
View Code? Open in Web Editor NEWUtilities for working with binary data and bit manipulation in Dart.
Home Page: https://pub.dev/packages/binary
License: MIT License
Utilities for working with binary data and bit manipulation in Dart.
Home Page: https://pub.dev/packages/binary
License: MIT License
... i.e. to create Uint3, Uint12, and other data structures I'd like!
(They don't make sense in this library, but letting users create their own seems OK)
<int32>.bitRange(4, 0)
should return the 4th bit to the 0th bit, but instead throws:
RangeError (size): Out of range. Must be > left (4).: 5
Something like:
BitPatternBuilder.parse('0101AAAAABBBCC')
Just a reminder to myself.
I suspect there will be more changes I want to get in before publishing.
Hi, i needed to do some bitmasking at compile time where possible and it would be great if the library provided constant for bits, for example:
class CompatibleBits
{
/// The largest possbile unsigned integer that is valid in JavaScript.
static const MAXJSUINT = ALLBITSSET53;
static const ALLBITSSET53 = 0x1fffffffffffff;
// Javascript shift operations works on 32 bit values only.
static const int bit1 = MAXJSUINT & 1 << 0;
static const int bit2 = MAXJSUINT & 1 << 1;
static const int bit3 = MAXJSUINT & 1 << 2;
static const int bit4 = MAXJSUINT & 1 << 3;
static const int bit5 = MAXJSUINT & 1 << 4;
static const int bit6 = MAXJSUINT & 1 << 5;
static const int bit7 = MAXJSUINT & 1 << 6;
static const int bit8 = MAXJSUINT & 1 << 7;
static const int bit9 = MAXJSUINT & 1 << 8;
static const int bit10 = MAXJSUINT & 1 << 9;
static const int bit11 = MAXJSUINT & 1 << 10;
static const int bit12 = MAXJSUINT & 1 << 11;
static const int bit13 = MAXJSUINT & 1 << 12;
static const int bit14 = MAXJSUINT & 1 << 13;
static const int bit15 = MAXJSUINT & 1 << 14;
static const int bit16 = MAXJSUINT & 1 << 15;
static const int bit17 = MAXJSUINT & 1 << 16;
static const int bit18 = MAXJSUINT & 1 << 17;
static const int bit19 = MAXJSUINT & 1 << 18;
static const int bit20 = MAXJSUINT & 1 << 19;
static const int bit21 = MAXJSUINT & 1 << 20;
static const int bit22 = MAXJSUINT & 1 << 21;
static const int bit23 = MAXJSUINT & 1 << 22;
static const int bit24 = MAXJSUINT & 1 << 23;
static const int bit25 = MAXJSUINT & 1 << 24;
static const int bit26 = MAXJSUINT & 1 << 25;
static const int bit27 = MAXJSUINT & 1 << 26;
static const int bit28 = MAXJSUINT & 1 << 27;
static const int bit29 = MAXJSUINT & 1 << 28;
static const int bit30 = MAXJSUINT & 1 << 29;
static const int bit31 = MAXJSUINT & 1 << 30;
static const int bit32 = MAXJSUINT & 1 << 31;
// We can use exended bits until 53 bits are used.
// shift is not working past 32 bits in javascript but
// we cant still use the precision bits.
static const int bit33 = 0x100000000;
static const int bit34 = 0x200000000;
static const int bit35 = 0x400000000;
static const int bit36 = 0x800000000;
static const int bit37 = 0x1000000000;
static const int bit38 = 0x2000000000;
static const int bit39 = 0x4000000000;
static const int bit40 = 0x8000000000;
static const int bit41 = 0x10000000000;
static const int bit42 = 0x20000000000;
static const int bit43 = 0x40000000000;
static const int bit44 = 0x80000000000;
static const int bit45 = 0x100000000000;
static const int bit46 = 0x200000000000;
static const int bit47 = 0x400000000000;
static const int bit48 = 0x800000000000;
static const int bit49 = 0x1000000000000;
static const int bit50 = 0x2000000000000;
static const int bit51 = 0x4000000000000;
static const int bit52 = 0x8000000000000;
static const int bit53 = 0x10000000000000;
}
I then can use these to give a name to options with these constants (sadly dart doesnt support constant getters or functions)
class Options
{
static const int option1 = CompatibleBits.bit1;
static const int option2 = CompatibleBits.bit2;
int getOptionsForThing()
{
return option1 | option2;
}
}
i would be great to have this on the library, without throwing or checks, as performant as possible.
Or with enums.
BitOps.prototype.not = function(x) {
var ret = ~x;
if (ret >= 0)
return ret;
else
return ret + 0x100000000;
};
These aren't less typing than Uint4(1)
, as
isn't technically true, and they don't provide a lot of value.
In the upgrade to v3.0.0
, the meta
package was upgraded to v1.8.0
, causing a conflict with the Flutter SDK in the stable
channel because Flutter depends on meta
v1.7.0
, but in the master
branch, it already depends on meta
v1.8.0
. So, which option is considered the best?
meta
version to v1.7.0
PS: Using dependency_overrides
we can evict the issue meanwhile.
I.e. the result of 0xffffffff + 0xffffffff
.
Most of the current bit operations have undefined behavior, especially across the VM and JS.
Please migrate to sound null safety.
Thanks.
Perhaps also add signedShiftLeft
.
Docs should also be updated so that <<
>>
and >>>
are correctly stated:
<<
Zero-fill left shift >>
Signed right shift>>>
Zero-fill right shift... as an easier way to replace a range of bits in an int
:
extension BinaryInt on int {
int replaceBitRange(int, left, int right, int bits);
}
void example(int bits) {
bits.replaceBitRange(4, 0, 0x1b);
}
... to clear a best practice/lint.
I.e.
bits.toggleBit(30, true /* or false */)
Currently implemented elsewhere as:
extension IntegralX<T extends Integral<T>> on Integral<T> {
T toggleBit(int bit, bool set) {
return set ? setBit(bit) : clearBit(bit);
}
}
print(psr.toBinaryPadded());
if (i.allowChangingFlags) {
psr = psr.replaceBitRange(31, 24, op.bitRange(31, 24).value);
}
print(psr.toBinaryPadded());
00000000000000000000000000010000
>>>>>
00000000000000000000000000000000
... it shouldn't have changed the lower bits at all.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.