www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - trouble with bit[]

reply "Regan Heath" <regan netwin.co.nz> writes:
# import std.stdio;
#
# void main()
# {
#     byte[] ascii = cast(byte[])"0000";
#     bit[] sms;
#     bit[] tmp;
#
#     writef("ASC:");
#     foreach(int i, bit bb; (cast(bit*)ascii.ptr)[0..32]) {
#         if (i && i%8 == 0) writef(" ");
#         writef("%d",bb);
#     }
#     writefln("");
#
#     foreach(int j, byte b; ascii) {
#         tmp = (cast(bit*)&b)[0..7];
#
#         writef("TMP:");
#         for(int k = 0; k < j; k++) writef("         ");
#         foreach(int i, bit b; tmp) writef("%d",b);
#         writefln("");
#
#         sms ~= tmp;
#
#         writef("SMS:");
#         foreach(int i, bit b; sms) {
#             if (i && i%7 == 0) writef("  ");
#             writef("%d",b);
#         }
#         writefln("");
#     }
#     writefln("");
# }

output:

ASC:00001100 00001100 00001100 00001100
TMP:0000110
SMS:0000110
TMP:         0000110
SMS:0000110  0010111
TMP:                  0000110
SMS:0000110  0010111  0010000
TMP:                           0000110
SMS:0000110  0010111  0010000  0100000

'tmp' is appended to 'sms' each time round the loop, but something is  
going very wrong.

Anyone got any ideas?

Regan
Mar 16 2005
next sibling parent Derek Parnell <derek psych.ward> writes:
On Thu, 17 Mar 2005 13:48:23 +1300, Regan Heath wrote:

 # import std.stdio;
 #
 # void main()
 # {
 #     byte[] ascii = cast(byte[])"0000";
 #     bit[] sms;
 #     bit[] tmp;
 #
 #     writef("ASC:");
 #     foreach(int i, bit bb; (cast(bit*)ascii.ptr)[0..32]) {
 #         if (i && i%8 == 0) writef(" ");
 #         writef("%d",bb);
 #     }
 #     writefln("");
 #
 #     foreach(int j, byte b; ascii) {
 #         tmp = (cast(bit*)&b)[0..7];
 #
 #         writef("TMP:");
 #         for(int k = 0; k < j; k++) writef("         ");
 #         foreach(int i, bit b; tmp) writef("%d",b);
 #         writefln("");
 #
 #         sms ~= tmp;
 #
 #         writef("SMS:");
 #         foreach(int i, bit b; sms) {
 #             if (i && i%7 == 0) writef("  ");
 #             writef("%d",b);
 #         }
 #         writefln("");
 #     }
 #     writefln("");
 # }
 
 output:
 
 ASC:00001100 00001100 00001100 00001100
 TMP:0000110
 SMS:0000110
 TMP:         0000110
 SMS:0000110  0010111
 TMP:                  0000110
 SMS:0000110  0010111  0010000
 TMP:                           0000110
 SMS:0000110  0010111  0010000  0100000
 
 'tmp' is appended to 'sms' each time round the loop, but something is  
 going very wrong.
 
 Anyone got any ideas?
 
 Regan

I think you found a bug. Here another way to show the same bug... <code> import std.stdio; void displayb(char[] name, bit[] x) { writef("%-5s: ", name); foreach(bit b; x) writef("%d",b); writefln(""); } void main() { bit[] a; bit[] b; a.length = 7; a[0] = 0; a[1] = 1; a[2] = 1; a[3] = 0; a[4] = 0; a[5] = 1; a[6] = 0; displayb("a", a); b ~= a; displayb("b1", b); b ~= a; displayb("b2", b); b.length = 0; b ~= a; displayb("b3", b); b.length = b.length + 7; for(int i = 0; i < a.length; i++) b[i+7] = a[i]; displayb("b4", b); } <code> -- Derek Melbourne, Australia 17/03/2005 12:12:15 PM
Mar 16 2005
prev sibling parent =?ISO-8859-15?Q?Thomas_K=FChne?= writes:
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Regan Heath wrote:

| # import std.stdio;
| #
| # void main()
| # {
| #     byte[] ascii = cast(byte[])"0000";
| #     bit[] sms;
| #     bit[] tmp;
| #
| #     writef("ASC:");
| #     foreach(int i, bit bb; (cast(bit*)ascii.ptr)[0..32]) {
| #         if (i && i%8 == 0) writef(" ");
| #         writef("%d",bb);
| #     }
| #     writefln("");
| #
| #     foreach(int j, byte b; ascii) {
| #         tmp = (cast(bit*)&b)[0..7];
| #
| #         writef("TMP:");
| #         for(int k = 0; k < j; k++) writef("         ");
| #         foreach(int i, bit b; tmp) writef("%d",b);
| #         writefln("");
| #
| #         sms ~= tmp;
| #
| #         writef("SMS:");
| #         foreach(int i, bit b; sms) {
| #             if (i && i%7 == 0) writef("  ");
| #             writef("%d",b);
| #         }
| #         writefln("");
| #     }
| #     writefln("");
| # }
|
| output:
|
| ASC:00001100 00001100 00001100 00001100
| TMP:0000110
| SMS:0000110
| TMP:         0000110
| SMS:0000110  0010111
| TMP:                  0000110
| SMS:0000110  0010111  0010000
| TMP:                           0000110
| SMS:0000110  0010111  0010000  0100000
|
| 'tmp' is appended to 'sms' each time round the loop, but something is
| going very wrong.
|
| Anyone got any ideas?

Added to DStress as
http://dstress.kuehne.cn/run/opCatAssign_09.d
http://dstress.kuehne.cn/run/opCatAssign_10.d

Thomas

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.0 (MingW32)

iD8DBQFCQ80y3w+/yD4P9tIRArSDAKDBCfpKkhA1hiqT82MMf5awHWRwAgCeOu2H
VF/lhXghcKeAsr8zYlUqY/4=
=um0P
-----END PGP SIGNATURE-----
Mar 25 2005