Unsinged types

Discussion in 'C Programming' started by Jase Schick, Jul 2, 2010.

  1. Jase Schick

    Jase Schick Guest

    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
     
    Jase Schick, Jul 2, 2010
    #1
    1. Advertising

  2. Jase Schick

    bart.c Guest

    Re: Unsigned types

    "Jase Schick" <> wrote in message
    news:i0lj51$hn3$...
    > 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
     
    bart.c, Jul 2, 2010
    #2
    1. Advertising

  3. Jase Schick

    Ian Collins Guest

    On 07/ 3/10 08:45 AM, 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.


    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
     
    Ian Collins, Jul 2, 2010
    #3
  4. Jase Schick

    Billy Mays Guest

    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?

    --
    Billy Mays
    http://www.jpgdump.com <- My attempt at humor.
     
    Billy Mays, Jul 2, 2010
    #4
  5. Jase Schick

    Ben Pfaff Guest

    Jase Schick <> writes:

    > 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.


    What makes you think that this is a 64-bit world? C runs on even
    very small systems.
    --
    Ben Pfaff
    http://benpfaff.org
     
    Ben Pfaff, Jul 2, 2010
    #5
  6. Jase Schick

    Eric Sosman Guest

    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
    lid
     
    Eric Sosman, Jul 2, 2010
    #6
  7. Jase Schick

    Nick Guest

    Billy Mays <> 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
     
    Nick, Jul 2, 2010
    #7
  8. 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
     
    Francois Grieu, Jul 3, 2010
    #8
  9. Jase Schick

    Gene Guest

    On Jul 2, 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


    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.
     
    Gene, Jul 3, 2010
    #9
  10. בת×ריך ×™×•× ×©×™×©×™, 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 passedrandom 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.
     
    Malcolm McLean, Jul 21, 2012
    #10
  11. Jase Schick

    John Bode Guest

    On Friday, July 2, 2010 8:34:10 PM UTC-5, Gene wrote:
    > On Jul 2, 4:45 pm, Jase Schick &lt;&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 tome.
    > &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.




    On Friday, July 2, 2010 8:34:10 PM UTC-5, Gene wrote:
    > On Jul 2, 4:45 pm, Jase Schick &lt;&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 tome.
    > &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.




    On Friday, July 2, 2010 8:34:10 PM UTC-5, Gene wrote:
    > On Jul 2, 4:45 pm, Jase Schick &lt;&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 tome.
    > &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.




    On Friday, July 2, 2010 8:34:10 PM UTC-5, Gene wrote:
    > On Jul 2, 4:45 pm, Jase Schick &lt;&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 tome.
    > &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.
     
    John Bode, Jul 23, 2012
    #11
  12. Jase Schick

    John Bode Guest

    On Friday, July 2, 2010 8:34:10 PM UTC-5, Gene wrote:
    > On Jul 2, 4:45 pm, Jase Schick &lt;&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 tome.
    > &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 anunsigned 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 typeavailable.
     
    John Bode, Jul 23, 2012
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joseph Suprenant

    Unsinged char to int

    Joseph Suprenant, Aug 18, 2003, in forum: C Programming
    Replies:
    2
    Views:
    418
    Jan Engelhardt
    Aug 18, 2003
  2. Kevin Goodsell
    Replies:
    30
    Views:
    827
    Dan Pop
    Oct 22, 2003
  3. Replies:
    14
    Views:
    917
    Mark McIntyre
    Jan 28, 2006
  4. Nevil Lesdog

    Comparing singed to unsinged warning

    Nevil Lesdog, Aug 23, 2007, in forum: C Programming
    Replies:
    7
    Views:
    403
    Ark Khasin
    Aug 24, 2007
  5. Jase Schick

    Unsinged types

    Jase Schick, Jul 19, 2012, in forum: C Programming
    Replies:
    35
    Views:
    980
    Tim Rentsch
    Sep 7, 2012
Loading...

Share This Page