Thread overview
XOR a bunch of data
Oct 17, 2005
Tiago Gasiba
Oct 17, 2005
Charles
Jul 08, 2008
Dave Akers
Jul 09, 2008
Koroskin Denis
October 17, 2005
Hi all,

  Why can't I do the following?

--- code ---
import std.c.stdio;
import std.c.stdlib;

int main( ){
  ulong[10] X, Y;
  X[] = 2;
  Y[] = 5;
  X[] ^= Y[];

  foreach( ulong u; X )
    printf("%lu\n",u);

  return 0;
}
--- code ---

The compiler complains:
test.d(8): slice expression X[] is not a modifiable lvalue
test.d(8): 'X[]' is not a scalar, it is a ulong[]
test.d(8): 'X[]' is not of integral type, it is a ulong[]
test.d(8): 'Y[]' is not of integral type, it is a ulong[]

What I want to do is simply this:

  for( int ii=0; ii<X.length; ii++ )
    X[ii] ^= Y[ii];

Am I not allowed to XOR a bunch of data like this X[] ^= Y[] ??? Thanks!

Best,
Tiago Gasiba
-- 
Tiago Gasiba (MSc.) - http://www.gasiba.de
Everything should be made as simple as possible, but not simpler.
October 17, 2005
> Am I not allowed to XOR a bunch of data like this X[] ^= Y[] ??? Thanks!

Not yet, those are whats been dubbed 'array arithematic operations' , and I think are planned for 2.0 ( hopefully sooner , can't really call them first-class arrays w/o em! ).

It would probably look like : X ^= Y ;

Thanks,
Charlie



"Tiago Gasiba" <tiago.gasiba@gmail.com> wrote in message news:dj0e3m$cku$1@digitaldaemon.com...
> Hi all,
>
>   Why can't I do the following?
>
> --- code ---
> import std.c.stdio;
> import std.c.stdlib;
>
> int main( ){
>   ulong[10] X, Y;
>   X[] = 2;
>   Y[] = 5;
>   X[] ^= Y[];
>
>   foreach( ulong u; X )
>     printf("%lu\n",u);
>
>   return 0;
> }
> --- code ---
>
> The compiler complains:
> test.d(8): slice expression X[] is not a modifiable lvalue
> test.d(8): 'X[]' is not a scalar, it is a ulong[]
> test.d(8): 'X[]' is not of integral type, it is a ulong[]
> test.d(8): 'Y[]' is not of integral type, it is a ulong[]
>
> What I want to do is simply this:
>
>   for( int ii=0; ii<X.length; ii++ )
>     X[ii] ^= Y[ii];
>
> Am I not allowed to XOR a bunch of data like this X[] ^= Y[] ??? Thanks!
>
> Best,
> Tiago Gasiba
> --
> Tiago Gasiba (MSc.) - http://www.gasiba.de
> Everything should be made as simple as possible, but not simpler.


July 08, 2008
Charles wrote:
>> Am I not allowed to XOR a bunch of data like this X[] ^= Y[] ???
>> Thanks!
> 
> Not yet, those are whats been dubbed 'array arithematic operations' , and I
> think are planned for 2.0 ( hopefully sooner , can't really call them
> first-class arrays w/o em! ).
> 
> It would probably look like : X ^= Y ;
> 
> Thanks,
> Charlie
> 
> 

Any idea how this will be implemented? something like

ArrayXor(ubyte[] X, ubyte[] Y) {
	for (int i=0; i<X.length; i++) {
		X[i] ^= Y[i % Y.length];
	}
}


> 
> "Tiago Gasiba" <tiago.gasiba@gmail.com> wrote in message
> news:dj0e3m$cku$1@digitaldaemon.com...
>> Hi all,
>>
>>   Why can't I do the following?
>>
>> --- code ---
>> import std.c.stdio;
>> import std.c.stdlib;
>>
>> int main( ){
>>   ulong[10] X, Y;
>>   X[] = 2;
>>   Y[] = 5;
>>   X[] ^= Y[];
>>
>>   foreach( ulong u; X )
>>     printf("%lu\n",u);
>>
>>   return 0;
>> }
>> --- code ---
>>
>> The compiler complains:
>> test.d(8): slice expression X[] is not a modifiable lvalue
>> test.d(8): 'X[]' is not a scalar, it is a ulong[]
>> test.d(8): 'X[]' is not of integral type, it is a ulong[]
>> test.d(8): 'Y[]' is not of integral type, it is a ulong[]
>>
>> What I want to do is simply this:
>>
>>   for( int ii=0; ii<X.length; ii++ )
>>     X[ii] ^= Y[ii];
>>

what if Y.length < X.length... array bounds error...


>> Am I not allowed to XOR a bunch of data like this X[] ^= Y[] ???
>> Thanks!
>>
>> Best,
>> Tiago Gasiba
>> --
>> Tiago Gasiba (MSc.) - http://www.gasiba.de
>> Everything should be made as simple as possible, but not simpler.
> 
> 
July 09, 2008
On Wed, 09 Jul 2008 02:43:12 +0400, Dave Akers <dragon@dazoe.net> wrote:

> Charles wrote:
>>> Am I not allowed to XOR a bunch of data like this X[] ^= Y[] ???
>>> Thanks!
>>  Not yet, those are whats been dubbed 'array arithematic operations' , and I
>> think are planned for 2.0 ( hopefully sooner , can't really call them
>> first-class arrays w/o em! ).
>>  It would probably look like : X ^= Y ;
>>  Thanks,
>> Charlie
>>
>
> Any idea how this will be implemented? something like
>
> ArrayXor(ubyte[] X, ubyte[] Y) {
> 	for (int i=0; i<X.length; i++) {
> 		X[i] ^= Y[i % Y.length];
> 	}
> }
>
>
>>  "Tiago Gasiba" <tiago.gasiba@gmail.com> wrote in message
>> news:dj0e3m$cku$1@digitaldaemon.com...
>>> Hi all,
>>>
>>>   Why can't I do the following?
>>>
>>> --- code ---
>>> import std.c.stdio;
>>> import std.c.stdlib;
>>>
>>> int main( ){
>>>   ulong[10] X, Y;
>>>   X[] = 2;
>>>   Y[] = 5;
>>>   X[] ^= Y[];
>>>
>>>   foreach( ulong u; X )
>>>     printf("%lu\n",u);
>>>
>>>   return 0;
>>> }
>>> --- code ---
>>>
>>> The compiler complains:
>>> test.d(8): slice expression X[] is not a modifiable lvalue
>>> test.d(8): 'X[]' is not a scalar, it is a ulong[]
>>> test.d(8): 'X[]' is not of integral type, it is a ulong[]
>>> test.d(8): 'Y[]' is not of integral type, it is a ulong[]
>>>
>>> What I want to do is simply this:
>>>
>>>   for( int ii=0; ii<X.length; ii++ )
>>>     X[ii] ^= Y[ii];
>>>
>
> what if Y.length < X.length... array bounds error...
>
>
>>> Am I not allowed to XOR a bunch of data like this X[] ^= Y[] ???
>>> Thanks!
>>>
>>> Best,
>>> Tiago Gasiba
>>> --
>>> Tiago Gasiba (MSc.) - http://www.gasiba.de
>>> Everything should be made as simple as possible, but not simpler.
>>

An exception will be thrown, I think:
BTW, operation may be done faster if 4 bytes is xored per step (or even 8-bytes per step on x64):
int* dst = cast(int*)X.ptr;
int* src = cast(int*)Y.ptr;

// xor
int steps = X.length / X[0].sizeof;
for (int i = steps; i >= 0; --i, ++src, ++dst) {
   *dst ^= *src;
}
// xor remaining byte-per-byte, code skipped.