casting int[] to bool[]

Bill Baxter wbaxter at gmail.com
Wed Jan 28 17:25:10 PST 2009


On Thu, Jan 29, 2009 at 10:20 AM, Saaa <empty at needmail.com> wrote:
>  int[] a = [1,2,3,0];
>  int[] aa = [0,1,0,1];
>  bool[] b = cast(bool[])a.dup;
>  bool[] bb = cast(bool[])aa.dup;
>  writefln(a,`-->`,b);
>  writefln(aa,`-->`,bb);
>
> --
>
> [1 2 3 0]-->[true false false false true false false false true false false
> false false false false false]
> [0 1 0 1]-->[false false false false true false false false false false
> false false true false false false]
>
>
> Why all this disagreeing?

bool is 1 byte under the hood.  Int is 4 bytes.
So what you are seeing is the 4 bytes of each int being treated as 4
separate bools in an ordering determined by the endian-ness of your
platform.

Casting arrays in this way is generally not a good idea.

You need to write a function that makes a fresh bool array out of your
int array.

--bb


More information about the Digitalmars-d-learn mailing list