468,502 Members | 2,200 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,502 developers. It's quick & easy.

Writing a incrementing/decrementing function with roll-over

Hi all,

I need to write a simple incrementing/decrementing function like this:

unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);

x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented or
decremented
Of course, the return value is the new value for x.

Some examples to clarify the question:
x=10,min=0,max=20,d=5 --15
x=10,min=0,max=20,d=-5 --5
x=18,min=0,max=20,d=5 --2
x=18,min=5,max=20,d=5 --7
x= 3,min=0,max=20,d=-5 --19
x=10,min=8,max=20,d=-5 --18

I'd like a function that use only unsigned char...

Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:

if( x+d>max )
x = max;
else
x += d;

I think it's not correct because what happens when x=200, d=100 and max=220??
If I write:

if( x>max-d )
x = max;
else
x += d;

what happens when max=10 and d=20?? I need a variable greater than unsigned
char to do the temporary sum x+d? And if I use long, I need extra-long
variables?
Thank you very much for the help.
Jul 13 '06 #1
10 4170
If you use wider types (with unsigned char to be the final target), it will
be easier.

E.g. use unsigned int instead of unsigned char.
Jul 13 '06 #2

"pozz" <pN************@libero.itwrote in message
news:Yp********************@twister1.libero.it...
Hi all,

I need to write a simple incrementing/decrementing function like this:

unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char
d);

x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented
or
decremented
Of course, the return value is the new value for x.

Some examples to clarify the question:
x=10,min=0,max=20,d=5 --15
x=10,min=0,max=20,d=-5 --5
x=18,min=0,max=20,d=5 --2
x=18,min=5,max=20,d=5 --7
x= 3,min=0,max=20,d=-5 --19
x=10,min=8,max=20,d=-5 --18

I'd like a function that use only unsigned char...

Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned
char).
x can't assume values greater than max, stored in another variable
(unsigned
char).
I usually write:

if( x+d>max )
x = max;
else
x += d;

I think it's not correct because what happens when x=200, d=100 and
max=220??
If I write:

if( x>max-d )
x = max;
else
x += d;

what happens when max=10 and d=20?? I need a variable greater than
unsigned
char to do the temporary sum x+d? And if I use long, I need extra-long
variables?
x = ( (d max) || (x max-d) ) ? max : x+d;
--
Fred L. Kleinschmidt
Boeing Associate Technical Fellow
Technical Architect, Software Reuse Project
Jul 13 '06 #3

pozz wrote:
Hi all,

I need to write a simple incrementing/decrementing function like this:

unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);

x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented or
decremented
Of course, the return value is the new value for x.

Some examples to clarify the question:
x=10,min=0,max=20,d=5 --15
x=10,min=0,max=20,d=-5 --5
x=18,min=0,max=20,d=5 --2
x=18,min=5,max=20,d=5 --7
x= 3,min=0,max=20,d=-5 --19
x=10,min=8,max=20,d=-5 --18

I'd like a function that use only unsigned char...

I won't directly help with this, but see below for a hint or two.
>
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:
You can normally do this:
x = (x + d) % (max+1);

Now using that, you should be able to figure
out a way to solve your problem above, right?

Post your attempt if it won't work

<snipped>

Jul 13 '06 #4
pozz wrote:
Hi all,

I need to write a simple incrementing/decrementing function like this:

unsigned char
change( unsigned char x, unsigned char min, unsigned char max, signed char d);

x is the value to increase/decrease
min is the minimum value that x can assume
max is the maximum value that x can assume
d is positive or negative and indicates how much x must be incremented or
decremented
Of course, the return value is the new value for x.

Some examples to clarify the question:
x=10,min=0,max=20,d=5 --15
x=10,min=0,max=20,d=-5 --5
x=18,min=0,max=20,d=5 --2
x=18,min=5,max=20,d=5 --7
x= 3,min=0,max=20,d=-5 --19
x=10,min=8,max=20,d=-5 --18

I'd like a function that use only unsigned char...
Hint: Solve the problem first when min=0. Make use
of the % operator.
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:

if( x+d>max )
x = max;
else
x += d;

I think it's not correct because what happens when x=200, d=100 and max=220??
If I write:
Are you worried that x+d may be larger than UCHAR_MAX ?
If that's the problem then there is an easy solution:

if ( x + d < x || x + d max ) x=max ;
else x += d ;

The point is that x+d has overflowed if and only if
x+d < x

Two remarks about the solution above:
1) It assumes that min=0. It will be easy to modify
it for when min>0.
2) It works for all kinds of unsigned integer types. The
only assumption is that x,d,max are all expressed in the
same unsigned integer type.
what happens when max=10 and d=20?? I need a variable greater than unsigned
char to do the temporary sum x+d? And if I use long, I need extra-long
variables?
No , you don't need to use larger "variables" than what x and d
are. You could solve the problem by using larger types but
what if the problem specified that you have to work with
unsigned long long ? You'd be in trouble then.
Dann Corbit wrote:
If you use wider types (with unsigned char to be the final target), it will
be easier.

E.g. use unsigned int instead of unsigned char.
Is unsigned int guaranteed to be wider than unsigned char ?
goose wrote:
You can normally do this:
x = (x + d) % (max+1);
This assumes that min=0 , right ?
Spiros Bousbouras

Jul 13 '06 #5
goose ha scritto:
pozz wrote:
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:

You can normally do this:
x = (x + d) % (max+1);
This is my attempt for the change function with roll-over:

unsigned char
change_roll( unsigned char x, unsigned char min, unsigned char max,
signed char d )
{
if( d>0 ) {
x -= min;
max -= min;
return (x+d)%(max+1) + min;
} else {
/* ??? */
}
}

What do I write in the else branch when d is negative?

And this is my attempt for the change function without roll-over:

unsigned char
inc( unsigned char x, unsigned char min, unsigned char max, signed char
d )
{
if( d>0 && (x+d<x || x+d>max) )
return max;
else if( d<0 && (x+d>x || x+d<min) )
return min;
else
return x+d;
}
What do you say about that?

Jul 14 '06 #6

pozz wrote:
And this is my attempt for the change function without roll-over:

unsigned char
inc( unsigned char x, unsigned char min, unsigned char max, signed char
d )
{
if( d>0 && (x+d<x || x+d>max) )
return max;
else if( d<0 && (x+d>x || x+d<min) )
return min;
else
return x+d;
}
What do you say about that?
You said in your first post that for the "without roll-over" part d is
an *unsigned* char but your definition says that d is a signed char.
Still , it looks correct to me.

Spiros Bousbouras

Jul 14 '06 #7
sp****@gmail.com ha scritto:
You said in your first post that for the "without roll-over" part d is
an *unsigned* char but your definition says that d is a signed char.
Oh, you are right. I generalized the function for increment and
decrement.

Still , it looks correct to me.
Hmm..., I rethought about that and I worry for the following line:
return (x+d)%(max+1) + min;
What happens if x=200, d=200, max=210, min=0 and the architecture uses
only byte (8-bit
microcontroller application)? The return value will be 144 but it is
wrong!

Jul 14 '06 #8

pozz wrote:
goose ha scritto:
pozz wrote:
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:
>
You can normally do this:
x = (x + d) % (max+1);

This is my attempt for the change function with roll-over:

unsigned char
change_roll( unsigned char x, unsigned char min, unsigned char max,
signed char d )
{
if( d>0 ) {
x -= min;
max -= min;
return (x+d)%(max+1) + min;
} else {
/* ??? */
}
}

What do I write in the else branch when d is negative?
It's not correct I'm afraid. Consider the case where
min=0 , max=199 , UCHAR_MAX=255 , x=199 , d=57

Then your function ought to return 56 but it will return 0.

Spiros Bousbouras

Jul 14 '06 #9

pozz wrote:
sp****@gmail.com ha scritto:
You said in your first post that for the "without roll-over" part d is
an *unsigned* char but your definition says that d is a signed char.

Oh, you are right. I generalized the function for increment and
decrement.

Still , it looks correct to me.

Hmm..., I rethought about that and I worry for the following line:
return (x+d)%(max+1) + min;
What happens if x=200, d=200, max=210, min=0 and the architecture uses
only byte (8-bit
microcontroller application)? The return value will be 144 but it is
wrong!
When I said "it looks correct to me" I was referring *only* to the code
I
quoted. I still believe it is correct. But as you noticed yourself the
other
piece of code is not correct although your counterexample is not
strictly
correct either. That's because you have declared d as signed char so if
you only have 8 bits then it cannot have the value 200. Nevertheless I
provided a counterexample in a different post.

You have to think about some elementary number theory with regards
to remainders in order to arrive at a correct function.

Spiros Bousbouras

Jul 14 '06 #10
sp****@gmail.com ha scritto:
pozz wrote:
goose ha scritto:
pozz wrote:
Another similar question. I have a variabile x (unsigned char) and I must
increment it. The increment is stored in another variable, d (unsigned char).
x can't assume values greater than max, stored in another variable (unsigned
char).
I usually write:

>
You can normally do this:
x = (x + d) % (max+1);
This is my attempt for the change function with roll-over:

unsigned char
change_roll( unsigned char x, unsigned char min, unsigned char max,
signed char d )
{
if( d>0 ) {
x -= min;
max -= min;
return (x+d)%(max+1) + min;
} else {
/* ??? */
}
}

What do I write in the else branch when d is negative?

It's not correct I'm afraid. Consider the case where
min=0 , max=199 , UCHAR_MAX=255 , x=199 , d=57

Then your function ought to return 56 but it will return 0.
Yes, you are right. I must check if (x+d) generates an overflows
(x+d<x).

if( d>=0 ) {
x -= min;
max -= min;
if( x+d<x )
return d - (max-x) + min;
else
return (x+d)%(max+1) + min;
} else
????

But what should I write when d is negative?

Jul 14 '06 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by J. Campbell | last post: by
reply views Thread by Matt Clepper | last post: by
26 posts views Thread by Bas Wassink | last post: by
3 posts views Thread by Thomas Scheiderich | last post: by
25 posts views Thread by Shannon Jacobs | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.