casting int[] to bool[]

Bill Baxter wbaxter at gmail.com
Wed Jan 28 17:30:46 PST 2009


On Thu, Jan 29, 2009 at 10:25 AM, Jarrett Billingsley
<jarrett.billingsley at gmail.com> wrote:
> On Wed, Jan 28, 2009 at 8:25 PM, Bill Baxter <wbaxter at gmail.com> wrote:
>> 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.
>
> Stop beating me to things when I'm in the middle of typing!

Heh heh.  I sensed this one was gonna be a race.  :-P

--bb


More information about the Digitalmars-d-learn mailing list