Thread: confusion between signed and unsigned values

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    347

    confusion between signed and unsigned values

    I am struggling to

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Such suspense

    What exactly confuses you?
    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
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Well, the signed ones include negative values, the unsigned not.

    EG
    signed char can be an integer value from -128 to 127.
    while unsigned char can be an integer value from 0 to 255.

    int can be an integer from -32'768 to +32'767
    while unsigned int can be an integer value from 0 to 65'535.

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by nerio View Post
    int can be an integer from -32'768 to +32'767
    while unsigned int can be an integer value from 0 to 65'535.
    That depends on which compiler you're using. Given the range you specified, I'm guessing that you're using Turbo C/Turbo C++. The range you showed represents the minimum allowed by the standard. Modern implementations often implement a much wider range, such as -2,147,483,648 to 2,147,483,647.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Elkvis
    The range you showed represents the minimum allowed by the standard. Modern implementations often implement a much wider range, such as -2,147,483,648 to 2,147,483,647.
    Well, almost. The minimum guaranteed INT_MIN is actually -32767, presumably to cater to sign & magnitude and one's complement representations.
    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

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    It is strange complete thing is not posted. Actually I get an unsigned value every one second. I need to take the difference of two samples and the difference can be negative or positive or equal. Since I was of the impression that unsigned has to be copied to unsigned I did that and ending up in complete logic failure with lot of additional code of lines. My simple question is can I copy the unsigned to signed and do the processing. The value iam receiving is of type unsigned 16 bit. Will the same logic applies for unsigned 32 bit as well. Please help.

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You could use a larger signed type to hold the result of the subtraction.

    You could also figure out which value is larger and simply subtract the smaller from it, thus ensuring the result will never be negative.
    Last edited by Matticus; 01-04-2016 at 06:13 PM.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    So in case if I receive unsigned 32 bit my maximum negative will be 0 - pow(2,32). My maximum signed for 32 bit is pow(2,32)/2. Hence signed 32 bit cannot store. So I require a 64 bit. Am I correct? But I don't have 64 data type. Maximum long is 32 bit only. As suggested by you two methods which is better, I mean less issues.

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    It sounds like you want to use the second method.

    In fact, by using the ternary operator, you can do this in one line of code.

  10. #10
    Registered User
    Join Date
    Feb 2012
    Posts
    347
    Quote Originally Posted by Matticus View Post
    It sounds like you want to use the second method.

    In fact, by using the ternary operator, you can do this in one line of code.
    Awesome idea i need to try that but very sad that i have to handle signed values like unsigned values.

  11. #11
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    You could use slew rate limitation. It means you limit the difference between consecutive samples.

    For illustration, let's assume B-bit integers, and two's complement format used for signed values. This means that for the unsigned type, the representable range is 0 to 2B-1, inclusive, and for the signed type, -2B-1 to 2B-1-1, inclusive. A possible slew rate limit could be 2b-1-1, which would mean that the maximum swing in the source from 0 to 2B-1 or vice versa would take three samples to occur in the output (two, if the range is one smaller). High fast peaks are rounded. But as long as successive changes in the input stays within the slew rate limit, the input is reproduced exactly.

    In practice, a function that implements a slew rate limit also needs one additional unsigned integer to track the rate-limited value:
    Code:
    #include <limits.h>
    #define SLEWLIMIT  (UINT_MAX/2U)
    
    #if SLEWLIMIT > INT_MAX || -SLEWLIMIT < INT_MIN
    #error SLEWLIMIT is too large for the signed int type.
    #endif
    
    int delta(unsigned int *const valueptr, const unsigned int sample)
    {
        const unsigned int value = *valueptr;
    
        if (sample > value) {
            if (sample - value < SLEWLIMIT) {
                *valueptr = sample;
                return sample - value;
            } else {
                *valueptr += SLEWLIMIT;
                return (int)SLEWLIMIT;
            }
        } else
        if (sample < value) {
            if (value - sample < SLEWLIMIT) {
                *valueptr = sample;
                return -(int)(value - sample);
            } else {
                *valueptr -= SLEWLIMIT;
                return -(int)SLEWLIMIT;
            }
        } else
            return 0;
    }
    You supply the above function with a pointer to the slew-rate-limited sample value (an unsigned int) and each new sample, and it tells you the slew-rate limited difference to the previous sample.

    If you use Linux or Mac OS X, or you have awk installed, you can see what a slew rate limitation does to an input signal. Create input-file with one integer sample per line, and run
    Code:
    awk 'BEGIN { R = 5 ; c = 0 ; n = 0 }
         NF>0  { p = c ; i = int($1) ; n += 1 ;
                 if (i - p > R) c =p + R;
                 else if (p - i > R) c = p - R;
                 else c = i;
                 printf "%d %s %d %d\n", n, $1, c, c-p
               }' input-file > output-file
    and the output-file will have a running index in the first column (sample number), the input sample in the second column, the slew-rate limited sample (same as *valueptr above) in the third column, and the slew-rate limited delta in the fourth column. The value of R is the slew rate limit (5 in the above example).

    It is very illustrative to use e.g. Gnuplot to compare the two,
    Code:
    gnuplot -p -e 'plot "output-file" u 1:2 t "original" w lines lc 1, "output-file" u 1:3 t "limited" w lines lc -1'

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. signed or unsigned?
    By ulillillia in forum C Programming
    Replies: 7
    Last Post: 05-08-2007, 01:06 AM
  2. Signed vs Unsigned int
    By osyrez in forum C++ Programming
    Replies: 18
    Last Post: 08-17-2006, 07:38 AM
  3. signed/unsigned int
    By X PaYnE X in forum C Programming
    Replies: 3
    Last Post: 06-10-2004, 10:58 AM
  4. unsigned char vs signed char and range of values
    By Silvercord in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2003, 01:30 PM
  5. signed vs unsigned
    By char in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 01:10 PM