Public Group

8 Bit Overflows... adding two unsigned chars

This topic is 4874 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey, Before I start I just want to say that this is for a lab I have to do later. But the professor puts it up before hand so we can do it and just come and get checked off. If you decide not to help me because it is "school work" that is your choice, but the TA's will try and help and probably end up giving the answer (they don't explain things well). But please read on first as I am not looking for the answer straight up, I want to learn about the concepts and be able to answer it myself. Again, I am not looking for an answer, just help in understanding the problem and stuff. The problem is as follows: We are using remote logon to a linux machine so this is all done in C with emacs and gcc on a Linux box. We have to design a function that computes all the overflows when trying to add unsigned characters between 0 and 256. I know that an overflow is evident when the result is the opposite sign of both operands. So in this case an overflow has occured if the result is negative. Well with an unsigned character you cannot check for this since it will always result in a value above or equal to zero. So am I correct in saying that if I transfer the data into a normal character and check for the sign I can see if an overflow occured or not? Here is my code...
#include <stdio.h>

int compute()
{
unsigned char x, y, result;
char res;
int count = 0;

for(x = 0; x < 256; x++)
{
for(y = 0; y < 256; y++)
{
result = x + y;
res = result;

if(res < 0)
count++;

if(y == 255)
break;
}

if(x == 255)
break;
}

return count;
}

int main(int argc, char **argv)
{
int i, n, result;

if(argc < 2)
{
printf("You need to supply a count!\n");
}
else
{
n = atoi(argv[1]);

for(i = 0; i < n; i++)
{
result = compute();
}

printf("Total wrongs is %d\n", result);
}

return(1);
}

The output is as followed... > ./lab2 256 Total wrongs is 32768 Is anyone willing to give me some insight into how to approach this problem? Again, I am just talking concepts here, I don't want you to write code for me. I have a feeling I am wrong and that I need to do a bitshift or something... I was testing individual results of two unsigned characters though and was getting some odd answers. I also noticed that there was something weird that happened. The result approaches 128 from 0 and then goes to -127 and goes to -1 from there... This happened when I printed the variable result with a %u operator. Thanks, John Edit/P.S. Forgot the return statement, sorry. [Edited by - Krisc on September 14, 2005 10:24:41 AM]

Share on other sites
I'm amazed that program even compiles - your compute() function doesn't include a return statement. Or did you forget to paste it?

If I understand you right you want to find out how many times a summation of two unsigned chars, ranging from 0 to 255, results in an overflow. You assign this sum to a signed char, but these only go from -127 to 128, so how can that char tell you if the sum is more than 255?

I suggest you talk to your teacher. You have some weird statements in that program (for instance the if(y == 255) break).

Share on other sites
Try thinking about this the other way around. You have two numbers x and y. What is the biggest number you could add to x and not overflow? If y is larger than that, then you have a problem.

Alan

Share on other sites
You might check this out

Testing for integer overflow

It was a very similar question I posted a few weeks ago.

Share on other sites
Alright,

Alan - Sorry, I just forgot to rewrite the return statement. I was copying from putty by hand.

Goldfish - The breaks are there because the for loop seemed to not be breaking correctly.

If I think about it backwards...

x + y <= 255 right?

So y always has to be:
y <= 255 - x

If I did that, kind of seems like the cheep/hack way out of the problem.

After checking the other thread I am confused as to why
(x+y)<y should work...?

Share on other sites
As far as (x + y) < x

First, I'm pretty sure this has to be for unsigned values.

If an overflow occurs, then the overflow is going to be less than both x and y.

for example

x = 255
y = 255

(255 + 255) < 255
(11111111 + 11111111) < 11111111
(1 11111110) < 11111111
But the overflowed 1 gets truncated so
(11111110) < 11111111 which is true
(254) < 255

If an overflow doesn't occur, the result will be greater than both x and y.

Share on other sites
I see, thanks!

I am using unsigned characters... so this should work then!

I am now getting 32640 overflows.

Share on other sites
Quote:
 Original post by KriscGoldfish - The breaks are there because the for loop seemed to not be breaking correctly.

The reason for that is that the condition in the for loop is that x and y have to be less than 256, but they'll never reach 256!

I would recommend declaring x and y as ints, and then checking wether x+y > 255 or not.

Share on other sites
I'm kind of curious, are you getting any kind of compile warnings or errors from your loops?

I find it hard to believe it is letting you write

unsigned char x
for(x = 0; x < 256; ++x)

since 256 is not a valid value for an 8-bit char.

Share on other sites
Quote:
 Original post by RattrapI'm kind of curious, are you getting any kind of compile warnings or errors from your loops?I find it hard to believe it is letting you writeunsigned char xfor(x = 0; x < 256; ++x)since 256 is not a valid value for an 8-bit char.

Yeah, no warnings or errors. But if I change it to x < 255 then 255 will never be checked. If I use <= then the loop gets stuck.

Goldfish - I am not allowed to do that I think as it would be overriding the purpose of the lab, which is to check an 8-bit char for overflow.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

• 10
• 11
• 13
• 9
• 11
• Forum Statistics

• Total Topics
634091
• Total Posts
3015435
×