www.digitalmars.com         C & C++   DMDScript  

D - why is byte signed and char unsigned ?

reply imr1984 <imr1984_member pathlink.com> writes:
surely D has got it the wrong way round?

Usually when people talk about 'bytes' they refer to areas of memory that have
yet to be given a specific meaning - so a byte should be unsigned.
Mar 14 2004
next sibling parent "C. Sauls" <ibisbasenji yahoo.com> writes:
imr1984 wrote:
 surely D has got it the wrong way round?
 
 Usually when people talk about 'bytes' they refer to areas of memory that have
 yet to be given a specific meaning - so a byte should be unsigned.

This is simple, as a quick read-through of 'http://www.digitalmars.com/d/type.html' would show you. D's 8-bit integral type is byte (or ubyte for unsigned). So when we say byte, we mean a 1-byte integer. That's it. Char is unsigned because its supposed to be used for text, and is Unicode complient. If you want bytes without a specific meaning, then use ubyte or the void[] special case. I've done this a few times myself, with no problem. -C. Sauls -Invironz
Mar 14 2004
prev sibling next sibling parent Vathix <vathix dprogramming.com> writes:
imr1984 wrote:
 surely D has got it the wrong way round?
 
 Usually when people talk about 'bytes' they refer to areas of memory that have
 yet to be given a specific meaning - so a byte should be unsigned.

I agree. C# has byte for unsigned and sbyte for signed. I think D has ubyte and byte to be consistent with short/int/long. -- Christopher E. Miller
Mar 14 2004
prev sibling next sibling parent reply "Matthew" <matthew stlsoft.org> writes:
"imr1984" <imr1984_member pathlink.com> wrote in message
news:c31i17$9qv$1 digitaldaemon.com...
 surely D has got it the wrong way round?

With byte, perhaps. IMO, byte should not be an integral type, in so far as it should not be able to take place in integer operations. However, I know I'm whistling in the wind on this one, as usual.
 Usually when people talk about 'bytes' they refer to areas of memory that

 yet to be given a specific meaning - so a byte should be unsigned.

Mar 14 2004
parent reply Andy Friesen <andy ikagames.com> writes:
Matthew wrote:
 IMO, byte should not be an integral type, in so far as it should not be able
 to take place in integer operations. However, I know I'm whistling in the
 wind on this one, as usual.

That's unusual. Why? -- andy
Mar 14 2004
next sibling parent reply "Matthew" <matthew stlsoft.org> writes:
"Andy Friesen" <andy ikagames.com> wrote in message
news:c31u6v$sjn$1 digitaldaemon.com...
 Matthew wrote:
 IMO, byte should not be an integral type, in so far as it should not be


 to take place in integer operations. However, I know I'm whistling in


 wind on this one, as usual.

That's unusual. Why?

Because I view a byte as an opaque unit of memory. I'd prefer separate 1-byte sized integers. This is how I have it in the STLSoft libraries, with ss_sint8_t, ss_uint8_t and ss_byte_t. Same goes for bool. In D I tend to do this typedef int boolean; which makes it a distinct type and hides implicit conversions.
Mar 14 2004
parent "Walter" <walter digitalmars.com> writes:
"Matthew" <matthew stlsoft.org> wrote in message
news:c31uik$tfc$1 digitaldaemon.com...
 Because I view a byte as an opaque unit of memory.

void[] and void* fill the role of opaque memory.
Apr 02 2004
prev sibling parent "Derek Parnell" <Derek.Parnell psyc.ward> writes:
On Sun, 14 Mar 2004 07:39:11 -0800 (15/Mar/04 02:39:11 AM)
, Andy Friesen <andy ikagames.com> wrote:

 Matthew wrote:
 IMO, byte should not be an integral type, in so far as it should not be  
 able
 to take place in integer operations. However, I know I'm whistling in  
 the
 wind on this one, as usual.

That's unusual. Why?

Could it be that a byte is a set of eight consecutive bits without structure, and an eight-bit integer is only one of many possible structures used to interpret the meaning in a specific 8-bit sequence. Thus a byte is just a group of bits, and an integer is a specific way of applying meaning to those bits. -- Derek
Mar 14 2004
prev sibling parent reply Mark T <Mark_member pathlink.com> writes:
In article <c31i17$9qv$1 digitaldaemon.com>, imr1984 says...
surely D has got it the wrong way round?

Usually when people talk about 'bytes' they refer to areas of memory that have
yet to be given a specific meaning - so a byte should be unsigned.

============================== the Windows SDK uses typedef unsigned char BYTE; ============================= C# Programmer's Reference byte The byte keyword denotes an integral type that stores values as indicated in the following table. Type Range Size byte 0 to 255 Unsigned 8-bit integer ====================================================== Glossary of Internet Terms: Byte A set of Bits that represent a single character. Usually there are 8 Bits in a Byte, sometimes more, depending on how the measurement is being made. ========================================================== I don't think I have ever needed a negative number that must fit into 8 bits but I guess there are cases. I tend to agree that a byte is a chunk of memory that is 8 bits wide. tangent: I did suggest a while back that D follow the C99 stdint.h lead and use int8_t, uint8_t, int16_t, etc (D could drop the _t ) for exact width types and let int and long match the typical C complier for the architecture (byte wouldn't be used). Such as DEC/Compaq/HP C/C++ for Alpha, Intel C/C++ for Itanium, dmc or VC for Win32, gcc for Linux-x86, etc.
Mar 14 2004
next sibling parent reply =?ISO-8859-1?Q?Sigbj=F8rn_Lund_Olsen?= <sigbjorn lundolsen.net> writes:
Mark T wrote:

 In article <c31i17$9qv$1 digitaldaemon.com>, imr1984 says...
 
surely D has got it the wrong way round?

Usually when people talk about 'bytes' they refer to areas of memory that have
yet to be given a specific meaning - so a byte should be unsigned.

I guess Walter followed Java where they are 8 bit signed integers because ============================== the Windows SDK uses typedef unsigned char BYTE;

While I agree in the sense that I generally think of '0 - 255' when thinking of a 'byte', it *is* consistent with the other integral types. And the Windows SDK is rarely an example to follow. Cheers, Sigbjørn Lund Olsen
Mar 14 2004
parent Charles Hixson <charleshixsn earthlink.net> writes:
Sigbjørn Lund Olsen wrote:
 Mark T wrote:
 
 In article <c31i17$9qv$1 digitaldaemon.com>, imr1984 says...

 surely D has got it the wrong way round?

 Usually when people talk about 'bytes' they refer to areas of memory 
 that have
 yet to be given a specific meaning - so a byte should be unsigned.

I guess Walter followed Java where they are 8 bit signed integers because ============================== the Windows SDK uses typedef unsigned char BYTE;

While I agree in the sense that I generally think of '0 - 255' when thinking of a 'byte', it *is* consistent with the other integral types. And the Windows SDK is rarely an example to follow. Cheers, Sigbjørn Lund Olsen

frequently be interpreted as a 8-bit integer, signed or unsigned. But the integer interpretations are definitely a secondary overlay on the underlying meaning. And were I to assign a language specific secondary meaning, it would be that array of bits. And I don't know which end of the byte should correspond to bit[0]...but they should be sequential. Probably the 0 bit would be the left-hand bit, and the one's position, when interpreted as an int, would be the right bit. OTOH, it's been a long time since I used a system where I used this relation much. Or could depend on it when going cross-platform.
Mar 15 2004
prev sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
Mark T wrote:

[...]
| A set of Bits
[...]
| Usually there are 8 Bits in a Byte, sometimes more
[...]

Never thought of that! Then a byte can not only take more than eight
bits, but also can be distributed over several places of storage, thereby
introducing the need of explicitely defining an order on them.

So long!  
Mar 15 2004
parent reply larry cowan <larry_member pathlink.com> writes:
In article <c348lp$1njk$1 digitaldaemon.com>, Manfred Nowak says...
Mark T wrote:

[...]
| A set of Bits
[...]
| Usually there are 8 Bits in a Byte, sometimes more
[...]

Never thought of that! Then a byte can not only take more than eight
bits, but also can be distributed over several places of storage, thereby

storage. It would not be spread in multiple places. Although you would go a ways to find a computer with other than 8-bit bytes, there are still 4-bit memories around. In the past I know of 7, 8, 12 and 16 bit ones with or without one or more indirectly accessible additional parity bits.
introducing the need of explicitely defining an order on them.

Have you heard of big-endian and little-endian?
Mar 15 2004
next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
On Mon, 15 Mar 2004 20:08:32 +0000, larry cowan wrote:

[...]
| A set of Bits


 A byte is generally the smallest individually addressible multibit unit
 pf storage.

This is quite a better definition.
 Have you heard of big-endian and little-endian?

These are only philosophical derivates of an already existing strict ordering imposed on the bits. A "set of Bits" has no ordering. So that defintion should be considered plain wrong :-) So long!
Mar 15 2004
prev sibling parent "Serge K" <skarebo programmer.net> writes:
 A byte is generally the smallest individually addressible multibit unit pf
 storage.  It would not be spread in multiple places.  Although you would go a
 ways to find a computer with other than 8-bit bytes, there are still 4-bit
 memories around.  In the past I know of 7, 8, 12 and 16 bit ones with or
without
 one or more indirectly accessible additional parity bits.

Don't look that far - many popular DSP have the smallest addressable unit >8bit. There are many 16 and 32 bit devices around. Some DSP have more odd word sizes (like 24 bit).
Mar 22 2004