www.digitalmars.com         C & C++   DMDScript  

D - [Bug] Bit arrays sliced and joined in the wrong places

reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Using DMD 0.79, Windows 98SE.

Slicing of bit arrays is broken.  The program below produces the output:

01110110 10101111 10110101 11011001 10000100 11111000 00100100
00000000 00000000 00000000 10101111 00000000 00000000 00000000
00000000 00000000 00000000 10101111 11011001 10000100 00000000
00000000 10000100 00000000 10101111 11011001 10000100 00000000
00000000 10000111 11100000 10101111 11011001 10000100 00000000
111

What seems to be happening is that the starting index is treated as a 
_byte_ index into each array, and the number of bits to transfer is the 
specified number rounded up to a multiple of eight.

However, assigning a single bit value across a slice seems to work fine.

The last line of output is a special case.  In the function call, the 
starting index is misinterpreted in the same way, but it gets the length 
right.

As you've probably worked out already, here's the expected output:

01110110 10101111 10110101 11011001 10000100 11111000 00100100
00011100 00000000 00000000 00000000 00000000 00000000 00000000
00011011 01010110 00000000 00000000 00000000 00000000 00000000
01011011 01010110 00000000 00000000 00000000 00000000 00000000
01011011 01010111 11100000 00000000 00000000 00000000 00000000
011

There is also a similar problem when trying to concatenate bit arrays 
using either ~ or ~=.

My guess is that there is a root cause of these bit array bugs, similar 
to one that brought about the previous bug with foreach.

Stewart.

----------

import std.c.stdio;
import std.random;

bit[56] data1, data2;

void writeBits(bit[] data) {
	foreach (int i, bit b; data) {
		putchar(b ? '1' : '0');
		if ((i & 7) == 7) putchar(' ');
	}
	putchar('\n');
}

int main() {
	rand_seed(0, 0);

	for (int b = 0; b < data1.length; b++) {
		data1[b] = cast(bit) (rand() % 2);
	}

	writeBits(data1);
	data2[3..7] = data1[1..5];
	writeBits(data2);
	data2[4..15] = data1[3..14];
	writeBits(data2);
	data2[1..4] = data2[5..8];
	writeBits(data2);
	data2[13..19] = data1[1];
	writeBits(data2);
	writeBits(data2[2..5]);

	return 0;
}

-- 
My e-mail is valid but not my primary mailbox, aside from its being the 
unfortunate victim of intensive mail-bombing at the moment.  Please keep 
replies on the 'group where everyone may benefit.
Feb 09 2004
parent "Walter" <walter digitalmars.com> writes:
You're right, slicing of bit arrays doesn't work yet.
Feb 13 2004