Array Indexing

James Dunne james.jdunne at gmail.com
Mon May 1 09:47:44 PDT 2006


Lionello Lunesu wrote:
> Frits van Bommel wrote:
> 
>> DigitalMars D compiler News wrote:
>>
>>> I have another problem (I'm kinda a newbie in D, I come from a .NET 
>>> background) when I index an array with a byte. I have this function 
>>> that returns a byte I use for index on an array. The thing is that 
>>> whan that byte value is over 0x7F where it would be negative if it 
>>> was a char the runtime raises an array bounds exception, I know a 
>>> workaround and that is to and the value with 0xFF but I shouldn't 
>>> have to (and if I perform an explicit cast it doesn't help it) since 
>>> it is already a byte. Anyone knows if this is correct behavior?
>>
>>
>> 'byte' is a signed 8-bit integer. Try ubyte instead, that's the 
>> unsigned variant.
> 
> 
> This one has also bitten me a couple of times. "byte" just feels 
> unsigned to me. All the "byte"-like typedefs I've encountered in C were 
> always "unsigned char".
> 
> L.

Agreed.  Byte isn't really an integer, it's more for storage.  Storage 
should not care about signedness, it should just be used for read, 
write, comparison, and bit-flipping operations.

-- 
-----BEGIN GEEK CODE BLOCK-----
Version: 3.1
GCS/MU/S d-pu s:+ a-->? C++++$ UL+++ P--- L+++ !E W-- N++ o? K? w--- O 
M--@ V? PS PE Y+ PGP- t+ 5 X+ !R tv-->!tv b- DI++(+) D++ G e++>e 
h>--->++ r+++ y+++
------END GEEK CODE BLOCK------

James Dunne



More information about the Digitalmars-d mailing list