Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Unsinged types

42 views
Skip to first unread message

Jase Schick

unread,
Jul 2, 2010, 4:45:21 PM7/2/10
to
Hi Does C still need unsigned types? Java manages perfectly well without
them. Do many people ever use unsigned types nowadays and if so why? In a
64-bit world, the extra range is rarely worth the hastle it seems to me.

Jase

bart.c

unread,
Jul 2, 2010, 4:57:49 PM7/2/10
to

"Jase Schick" <n...@spam.invalid> wrote in message
news:i0lj51$hn3$1...@speranza.aioe.org...

> Hi Does C still need unsigned types? Java manages perfectly well without
> them. Do many people ever use unsigned types nowadays and if so why? In a
> 64-bit world, the extra range is rarely worth the hastle it seems to me.

Suppose you wanted to store a billion numbers in the range 0 to 200.

Would you rather use only 1GB (using unsigned char), or 8GB (using 64-bit
signed)?

--
Bartc

Ian Collins

unread,
Jul 2, 2010, 5:03:43 PM7/2/10
to

It's not just the range that differentiates signed and unsigned types.

Unsigned types are required anywhere a "bag of bits" is needed. That
might be for a bit field, representing a physical device, operands for
boolean operations and many other uses.

--
Ian Collins

Richard Heathfield

unread,
Jul 2, 2010, 5:04:25 PM7/2/10
to
Jase Schick wrote:
> Hi Does C still need unsigned types?

Yes.

> Java manages perfectly well without them.

I'm prepared to believe you unless evidence to the contrary turns up,
because it's easier than checking.


> Do many people ever use unsigned types nowadays and if so why?

Yes. For me at least, the most important property of an unsigned integer
type is that it cannot, absolutely *cannot*, overflow.

> In a
> 64-bit world, the extra range is rarely worth the hastle it seems to me.

For me, the range isn't the issue.

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
"Usenet is a strange place" - dmr 29 July 1999
Sig line vacant - apply within

Ben Pfaff

unread,
Jul 2, 2010, 5:07:33 PM7/2/10
to
Jase Schick <n...@spam.invalid> writes:

What makes you think that this is a 64-bit world? C runs on even
very small systems.
--
Ben Pfaff
http://benpfaff.org

Billy Mays

unread,
Jul 2, 2010, 5:07:11 PM7/2/10
to


I have never used an __unsung__ type. Whats it like?

--
Billy Mays
http://www.jpgdump.com <- My attempt at humor.

Richard Heathfield

unread,
Jul 2, 2010, 5:30:49 PM7/2/10
to
Billy Mays wrote:
> On 7/2/2010 4:45 PM, Jase Schick wrote:
>> Hi Does C still need unsigned types? Java manages perfectly well without
>> them. Do many people ever use unsigned types nowadays and if so why? In a
>> 64-bit world, the extra range is rarely worth the hastle it seems to me.
>>
>> Jase
>
>
> I have never used an __unsung__ type. Whats it like?

Heroic.

Eric Sosman

unread,
Jul 2, 2010, 5:54:28 PM7/2/10
to
On 7/2/2010 4:45 PM, Jase Schick wrote:
> Hi Does C still need unsigned types?

Yes. (Although "unsinged" types are something of a gray area.)

> Java manages perfectly well without
> them.

No, on two counts. First, Java does in fact have an unsigned
integer type. Second, Java's lack of other unsigned types forces
the programmer into silly make-work, with concomitant opportunity
for errors.

> Do many people ever use unsigned types nowadays and if so why?

Yes. The unsigned type I personally use most frequently is
size_t, but sometimes other situations arise where I want to
represent quantities that are necessarily non-negative. Also,
there are the "bag of bits" situations when I want to view an
integer as a collection of flags or small fields (the "unsinged"
or "raw" types are perfect for this). Finally, there are mixed
situations where an integer represents a quantity but must be
manipulated as a bag of bits.

> In a
> 64-bit world, the extra range is rarely worth the hastle it seems to me.

I disagree with the implied premise, but since the argument
seems to be a pure non sequitur my disagreement scarcely matters.

--
Eric Sosman
eso...@ieee-dot-org.invalid

Nick

unread,
Jul 2, 2010, 5:58:41 PM7/2/10
to
Billy Mays <no...@nohow.com> writes:

> On 7/2/2010 4:45 PM, Jase Schick wrote:
>> Hi Does C still need unsigned types? Java manages perfectly well without
>> them. Do many people ever use unsigned types nowadays and if so why? In a
>> 64-bit world, the extra range is rarely worth the hastle it seems to me.
>>
>> Jase
>
>
> I have never used an __unsung__ type. Whats it like?

Not very good. At least, I've never heard anyone praising it.
--
Online waterways route planner | http://canalplan.eu
Plan trips, see photos, check facilities | http://canalplan.org.uk

Francois Grieu

unread,
Jul 2, 2010, 7:23:00 PM7/2/10
to
On 02/07/2010 22:45, Jase Schick wrote:
> Hi Does C still need unsigned types? Java manages perfectly well without
> them.

Here is the first array declaration in the alphabetically first Java source file that I found in the samples folder supplied by Sun for Java for Smart Cards (java_card_kit-2_2_2/samples/src/com/sun/javacard/samples/odSample/packageA/A.java) as I use it

//BApp1AID is the Applet AID for applet BApp1 in packageB
public static byte[] BApp1AID={(byte)0xA0, (byte)0x00,
(byte)0x00, (byte)0x00,
(byte)0x62, (byte)0x03,
(byte)0x01, (byte)0x0C,
(byte)0x07, (byte)0x02,
(byte)0x01};

The first (byte) cast is necessary else that won't compile; thus in Java as practiced in my professional field, it is customary to put (byte) on every byte constant, just as in this example code; and yes, any data processed comes and goes as bytes, there is no other option. Also, BApp1AID[0], which is the byte oh-x-ah-zero, is SMALLER then any other byte in BApp1AID. Now is that "perfectly well"?

> Do many people ever use unsigned types nowadays

Yes.

> and if so why?

For the same reason childs use unsigned numbers: they are simpler to grasp and manipulate. Mathematicians call them "naturals" for a good reason. The result of standard arithmetic operations on C unsigneds is clearly defined and matches a common mathematical notion (modulo UINT_MAX). For many real-life algorithms (e.g. image/video/data compression, extended precision arithmetic, cryptography, data communication), things are most naturally expressed with unsigneds. On many processors that sell by the hundred millions, hardware support for signeds is minimal. I have an example of a silicon manufacturer for SIM card CPUs that dropped support for signed comparison in the instruction set of a CPU introduced circa 2008, otherwise largely binary-compatible with the previous model that had this "feature". Therefore, using signeds on this architecture is considered criminal (an explicit option is needed to make the C compiler generate the ugly code that correctly compares signed var
iables).

> In a 64-bit world,

Which is that of the main CPUs of modern PCs, though not half of these are used in 64-bit mode. And they represent a tiny fraction of CPUs. Do you think your keyboard, mouse, screen have a 64-bit arithmetic unit ? Likely, not even 16, except perhaps in the address path (and that one is unsigned). Or that nobody programed these CPUs ?

> the extra range is rarely worth the hastle it seems to me.

True in that context for individual variables used as counter or indexes. For the rest, size and unsignedness often matters.


Francois Grieu

Gene

unread,
Jul 2, 2010, 9:34:10 PM7/2/10
to

Yes. If you write code close to hardware like graphics and network
protocol stuff, the lack of unsigned types quickly becomes _the_ most
annoying thing about Java. It was hubris for the Java language
designers to leave them out. A trivial example is that the 4 bytes in
a RGBA pixel each represent a quantity in [0..255] When you e.g. read
back a frame buffer, you must play stupid, inefficient tricks to do
useful computations with the data.

Jonathan Leffler

unread,
Jul 4, 2010, 1:09:13 AM7/4/10
to
On 7/2/10 2:07 PM, Billy Mays wrote:
> On 7/2/2010 4:45 PM, Jase Schick wrote:
>> Hi Does C still need unsigned types? Java manages perfectly well without
>> them. Do many people ever use unsigned types nowadays and if so why? In a
>> 64-bit world, the extra range is rarely worth the hastle it seems to me.
>
> I have never used an __unsung__ type. Whats it like?


The types referred to in the subject are not so much unsung as unburnt,
as in not even marginally singed.

-=JL=-

Gordon Burditt

unread,
Jul 20, 2012, 8:42:27 PM7/20/12
to
> Hi Does C still need unsigned types?

Yes. Among other things, it's difficult to do multi-precision
math with signed integers, given the properties that C gives it.
The modulo-arithmetic properties of unsigned integers are very
useful if you need, say, multi-megabit integers.

> Java manages perfectly well without
> them.

It's my bet that C needs unsigned types *to implement Java*. And
no, I'm not trying to nitpick about the type of sizeof() or the
argument of malloc(). I'm thinking largely about the BigInteger
types in Java that you need to be able to do math with.

In the openjdk 7u4 distribution, I see 1998 lines in *.c files
and 955 lines in *.cpp files containing 'unsigned'.

> Do many people ever use unsigned types nowadays and if so why? In a
> 64-bit world, the extra range is rarely worth the hastle it seems to me.

Everyone doesn't live in a 64-bit world. Some of the embedded processor
programmers haven't gotten past the 16-bit world yet.


Try keeping anything secret using RSA encryption with only 64-bit
keys. Even 1024 bits is marginal.

Robert Wessel

unread,
Jul 21, 2012, 1:38:50 AM7/21/12
to
Heck, many haven't gotten *to* the 16-bit world yet.

Malcolm McLean

unread,
Jul 21, 2012, 2:31:29 AM7/21/12
to
בתאריך יום שישי, 2 ביולי 2010 22:54:28 UTC+1, מאת Eric Sosman:
> On 7/2/2010 4:45 PM, Jase Schick wrote:
> Yes. The unsigned type I personally use most frequently is
> size_t, but sometimes other situations arise where I want to
> represent quantities that are necessarily non-negative.
>
That's one of my main beefs with size_t.

Often quantities are necessarily non-negative. So a negative number can be used to represent an error value. It's also a garbage detector. A random sequence of bits has a 50% of being a negative number. So assert(Nemployees >= 0) is practically certain to trigger after a few cycles of being passed random numbers. You can sanity test an unsigned, assert(Nemployees < 1000000), but that often leads to other issues.

Then numbers can be inherently non-negative, but intermediates may be negative.


Message has been deleted

John Bode

unread,
Jul 23, 2012, 6:12:45 PM7/23/12
to
On Friday, July 2, 2010 8:34:10 PM UTC-5, Gene wrote:
> On Jul 2, 4:45 pm, Jase Schick &lt;n...@spam.invalid&gt; wrote:
> &gt; Hi Does C still need unsigned types? Java manages perfectly well without
> &gt; them. Do many people ever use unsigned types nowadays and if so why? In a
> &gt; 64-bit world, the extra range is rarely worth the hastle it seems to me.
> &gt;
> &gt; Jase
>
> Yes. If you write code close to hardware like graphics and network
> protocol stuff, the lack of unsigned types quickly becomes _the_ most
> annoying thing about Java. It was hubris for the Java language
> designers to leave them out. A trivial example is that the 4 bytes in
> a RGBA pixel each represent a quantity in [0..255] When you e.g. read
> back a frame buffer, you must play stupid, inefficient tricks to do
> useful computations with the data.

Had to play with some geospatial data files (DTED) last year; not having an unsigned data type made the job *much* more annoying than it needed to be, especially when I was trying to display the bitmap. Lots of casts, lots of conversions that wouldn't have been necessary if I'd had an unsigned type available.
0 new messages