Why unsigned char?

This is a discussion on Why unsigned char? within the C Programming forums, part of the General Programming Boards category; Why unsigned char is necessary? What is its use exactly?...

  1. #1
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278

    Why unsigned char?

    Why unsigned char is necessary? What is its use exactly?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    I do not know about necessary, but unsigned char can be useful when you want to work with a byte, especially with respect to bitwise operations. What motivated you to ask your questions?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    It is important when you don't want your program to store negative values.
    Spidey out!

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    signed char, or char by default has a range of -128 to 127
    unsigned char has a range of 0-255
    if you need to work with numbers larger than 127 using only a single byte, you need unsigned char
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by abachler View Post
    signed char, or char by default has a range of -128 to 127
    unsigned char has a range of 0-255
    if you need to work with numbers larger than 127 using only a single byte, you need unsigned char
    Just keep in mind that whether a char is signed or unsigned is implementation-defined.
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  6. #6
    Anirban Ghosh
    Join Date
    Jan 2006
    Posts
    278
    NO still why unsigned.....and in case of signed why sign bit is necessary?

  7. #7
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,182
    How about actually reading the replies?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,699
    Quote Originally Posted by anirban View Post
    NO still why unsigned.....and in case of signed why sign bit is necessary?
    Why are you still asking this question?
    Code:
    bool fun(bool value)
    {
        return std::pow(std::exp(1), std::complex<float>(0, 1) 
        * std::complex<float>(std::atan(1)*(1 << (value + 2))))
        .real() > 0;
    }

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    ...

    Maybe because the computer needs it to determine if it means 255 or -1?

  10. #10
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    unsigned char also has no padding bits, so you can use it to copy all the bits of an object from one location to another. char, signed char, and all the other types except the optional fixed-width extended integer types might have padding, and might trap when used as the type to read another type's bytes.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by anirban View Post
    NO still why unsigned.....and in case of signed why sign bit is necessary?
    For the same reasons why unsigned short, unsigned int & unsigned long are necessary.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User
    Join Date
    Aug 2009
    Posts
    7
    srand() fuction needs unsigned int as argument, at least was what I've read in my book :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-16-2006, 09:23 PM
  2. Obtaining source & destination IP,details of ICMP Header & each of field of it ???
    By cromologic in forum Networking/Device Communication
    Replies: 1
    Last Post: 04-29-2006, 02:49 PM
  3. problem with reading and writing
    By yahn in forum C++ Programming
    Replies: 2
    Last Post: 01-03-2006, 03:38 PM
  4. Swap a bit
    By mr_nice! in forum C Programming
    Replies: 7
    Last Post: 03-01-2004, 02:15 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 12:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21