9.4 Unsigned types in C


9.4 Unsigned types in C#

Unsigned types are types which cannot store values in the negative range (hence unsigned “ the values stored are always positive). [4] The unsigned integral types in C# are: byte ( not ubyte !), ushort , uint , and ulong . [5]

[4] The reason for unsigned types is that you can store twice as many values in the positive range with the same number of bits because one bit is no longer required for storing the sign. Signed types always 'waste' one bit for determining if the value stored is positive or negative. There is no way in Java we can 'save' on the wasted bit, even if we know for certain that a particular value can never be negative. Anyway, this is probably not a significant issue given the amount of memory available to applications today.

[5] C/C++ programmers: there is no separate unsigned keyword in C#.

Attempting to assign a negative value to an unsigned numeric type results in a compilation or runtime error, depending on when the assignment is performed. For example, the statement

 uint i = -3; 

results in a compilation error:

 Constant value '-3' cannot be converted to a 'uint' 


From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net