Thread overview | |||||
---|---|---|---|---|---|
|
March 17, 2005 trouble with bit[] | ||||
---|---|---|---|---|
| ||||
# 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 |
March 17, 2005 Re: trouble with bit[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath | 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 |
March 25, 2005 Re: trouble with bit[] | ||||
---|---|---|---|---|
| ||||
Posted in reply to Regan Heath Attachments: | 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 |
Copyright © 1999-2021 by the D Language Foundation