www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - [bug?] dynamic bit array append

reply novice2 <novice2_member pathlink.com> writes:
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");
}
Jan 05 2005
next sibling parent reply =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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:

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
Jan 06 2005
next sibling parent reply novice2 <novice2_member pathlink.com> writes:
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.
Jan 06 2005
parent =?ISO-8859-1?Q?Anders_F_Bj=F6rklund?= <afb algonet.se> writes:
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
Jan 06 2005
prev sibling parent Stewart Gordon <smjg_1998 yahoo.com> writes:
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"

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.
Jan 12 2005
prev sibling next sibling parent Chris Sauls <Chris_member pathlink.com> writes:
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
Jan 06 2005
prev sibling parent reply "Lionello Lunesu" <lionello.lunesu crystalinter.remove.com> writes:
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.
Jan 07 2005
parent reply "Simon Buchan" <not a.valid.address.com> writes:
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

 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
Jan 07 2005
parent novice2 <novice2_member pathlink.com> writes:
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

 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.
Jan 12 2005