Thread: Why unsigned char?

  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
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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,195
    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

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(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,229
    How about actually reading the replies?

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(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, 04:38 PM
  4. Swap a bit
    By mr_nice! in forum C Programming
    Replies: 7
    Last Post: 03-01-2004, 03:15 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM