Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
January 06, 2005 [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Hello. I met problem - i can't append one bit to bit array. Is it bug or i missed something? My environment: Windows XP, DMD 0.110 Small test program: /*********** "version = v_set" works properly. it print: bits:111111111111 "version = v_append" not works. it print: bits:100000001000 Sorry, if i missed something *********/ //version = v_set; version = v_append; void main() { bit[] bits; version(v_set) { bits.length = 12; for( int i=0; i<12; i++) bits[i] = 1; } version(v_append) { for( int i=0; i<12; i++) bits ~= 1; } printf( "bits:"); foreach( bit b; bits) printf("%d",b); printf("\n"); } |
January 06, 2005 Re: [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | novice2 wrote: > I met problem - i can't append one bit to bit array. > Is it bug or i missed something? Bug or "missing feature", you be the judge... > My environment: Windows XP, DMD 0.110 Compiling with GDC 0.9 gives an error: "sorry; can't append to array of bit" > Small test program: [snip] If you change the "bit" to "byte" instead, your small test program works just fine ? It's (yet another) thing missing from bit arrays, along with e.g. taking pointers to bits in arrays... --anders |
January 06, 2005 Re: [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | > >Bug or "missing feature", you be the judge... > it is a pity :( >If you change the "bit" to "byte" instead, >your small test program works just fine ? yes. i already used this "bypass". thanks, anders. |
January 06, 2005 Re: [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | novice2 wrote: >>If you change the "bit" to "byte" instead, >>your small test program works just fine ? > > yes. i already used this "bypass". It's the D way. Sometimes the default type is not enough, so you need to use the two wide variants that are also being provided... For instance, a "char" will hold any ASCII character but if you need to support Unicode you need a "wchar". And then, when you also need to be aware of surrogates* you need "dchar" Similarly, when you need a "bit" type that you can adress in arrays you need to use the "wbit" (byte) type. And for speed, you might want "dbit" (int) that for instance Object's opEquals() method uses. And yes, it is somewhat confusing that there are three string types and three boolean types in D. But it is also rather powerful, a trade-off there. --anders * See http://www.unicode.org/faq/utf_bom.html#34 |
January 06, 2005 Re: [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | In article <criq09$17hn$1@digitaldaemon.com>, novice2 says... >version(v_set) >{ >bits.length = 12; >for( int i=0; i<12; i++) bits[i] = 1; >} > >version(v_append) >{ >for( int i=0; i<12; i++) bits ~= 1; >} As an aside, if you are actually doing this in your project (wanting to set all elements to 1) you might prefer the following anyhow: # # bit[] bits; # # void main() { # bits.length = 12; # bits[] = 1; # } # -- Chris Sauls -- invironz |
January 07, 2005 Re: [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Posted in reply to novice2 | Hi. "novice2" <novice2_member@pathlink.com> wrote in message news:criq09$17hn$1@digitaldaemon.com... > "version = v_set" works properly. > it print: > > bits:111111111111 > > "version = v_append" not works. > it print: > > bits:100000001000 It makes sense actually: for the dynamic arrays, D only tracks the size of the array in bytes, it doesn't keep an index of the last bit. Appending a bit apparently appends a byte, always. I guess appending to a bit array should be simply forbidden (like in gdc, if I'm correct). (But maybe it's useful to be able to append multiple of 8 bits at a time) L. |
January 07, 2005 Re: [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Posted in reply to Lionello Lunesu | On Fri, 7 Jan 2005 10:56:56 +0200, Lionello Lunesu <lionello.lunesu@crystalinter.remove.com> wrote: > Hi. > > "novice2" <novice2_member@pathlink.com> wrote in message > news:criq09$17hn$1@digitaldaemon.com... >> "version = v_set" works properly. >> it print: >> >> bits:111111111111 >> >> "version = v_append" not works. >> it print: >> >> bits:100000001000 > > It makes sense actually: for the dynamic arrays, D only tracks the size of > the array in bytes, it doesn't keep an index of the last bit. Appending a > bit apparently appends a byte, always. > > I guess appending to a bit array should be simply forbidden (like in gdc, if > I'm correct). (But maybe it's useful to be able to append multiple of 8 bits > at a time) > > L. > Since bit arrays seem to only be supported on byte boundries, if at all, why bother? I don't know about you, but how much better is > bit[8*3] foo = \b10100011_10011011; // Or whatever the syntax is than > ubyte[3] bar = \xb39c; // I assume my bin->hex isn't off today? since neither are that readable by humans. (yes, flags, but AFAIK those are just as easy to do with bit-masks, if more verbose in some cases) -- "Yes, the american troops have advanced further. This will only make it easier for us to defeat them" - Iraqi Information Minister Muhammed Saeed al-Sahaf |
January 12, 2005 Re: [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Posted in reply to Simon Buchan | >Since bit arrays seem to only be supported on byte boundries, if at all, why bother? I wanted avoid ugly and bad readable by humans "calc mask, shift, or" constructions. As i work with bits, so i wanted to work with bits, not bytes. And i found in D language datatype "bit" and bit[] - good! By it has restrictions - bad :( > I don't know about you, but how much better is >> bit[8*3] foo = \b10100011_10011011; // Or whatever the syntax is >than >> ubyte[3] bar = \xb39c; // I assume my bin->hex isn't off today? I know hex. But it is not good-looking. Algorithm needs calculate bits one by one and append it to bits string. |
January 12, 2005 Re: [bug?] dynamic bit array append | ||||
---|---|---|---|---|
| ||||
Posted in reply to Anders F Björklund | Anders F Björklund wrote: > novice2 wrote: > >> I met problem - i can't append one bit to bit array. >> Is it bug or i missed something? > > > Bug or "missing feature", you be the judge... On DMD, the fact it's doing it wrong shows that it's a bone fide bug. >> My environment: Windows XP, DMD 0.110 > > Compiling with GDC 0.9 gives an error: > "sorry; can't append to array of bit" <snip> On GDC, OTOH, it's a missing feature. Of course, you could call it a bug that the feature is missing in GDC. But that's still better than having a completely broken implementation. But I thought last time I knew it was working to append one bit, just not to concatenate whole bit arrays. But I'll check again. I have a testcase at home.... Stewart. -- My e-mail is valid but not my primary mailbox. Please keep replies on the 'group where everyone may benefit. |
Copyright © 1999-2021 by the D Language Foundation