reinterpret array
ketmar via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Tue Jan 13 13:10:18 PST 2015
On Tue, 13 Jan 2015 20:52:13 +0000
Dominikus Dittes Scherkl via Digitalmars-d-learn
<digitalmars-d-learn at puremagic.com> wrote:
> On Tuesday, 13 January 2015 at 20:11:45 UTC, anonymous wrote:
> > On Tuesday, 13 January 2015 at 20:00:57 UTC, Dominikus Dittes
> > Scherkl wrote:
> >> So if I have a function that allowes to do this:
> >>
> >> uint a;
> >> a.bit[16] = true;
> >> writeln(a); // 65536
> >>
> >> Is it also already available?
> >
> > a |= 1 << 16;
>
> Of course you can calculate it, but the
> syntax looks quite different if you want to do
> a.bit[22] = false:
>
> a &= ~(1<<16);
>
> Or if you want to test a bit:
>
> if(a.bit[16])
>
> instead of
>
> if(a & (1<<16))
>
> much more convenient for arrays:
>
> ulong[100] a;
>
> a.bit[3000] = true;
>
> doing this directly with shifts is lousy (and error prone)
>
> But ok. I see, it's not really awesome :-/
it's not better than using something like this:
a.bitSet(22);
a.bitReset(30);
if (a.bit(16)) { ... }
you can easily do this with UFCS. you can even write some templates to
convert it to bit operations in compile time.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.puremagic.com/pipermail/digitalmars-d-learn/attachments/20150113/5f732148/attachment.sig>
More information about the Digitalmars-d-learn
mailing list