# General modulo-question

Discussion in 'Java' started by Christian Kruggel, Jul 7, 2003.

1. ### Christian KruggelGuest

Hi!

I try to do some basic programming and got stuck with the
modulo-operator an the problem how to shift any number n into the
interval [b;e] with b < 0 and e > 0.

My first approach was to calculate the spread of [b;e] by e - b and
then to calculate n % spread. Unfortunately this just works for
numbers that are already in [b;e] while other numbers smaller than b
and greater than e are not shifted correctly.

Has anybody programmed such calculation already and could give me a
helping hint?

Christian

Christian Kruggel, Jul 7, 2003

Christian Kruggel wrote:
> Hi!
>
> I try to do some basic programming and got stuck with the
> modulo-operator an the problem how to shift any number n into the
> interval [b;e] with b < 0 and e > 0.
>
> My first approach was to calculate the spread of [b;e] by e - b and
> then to calculate n % spread. Unfortunately this just works for
> numbers that are already in [b;e] while other numbers smaller than b
> and greater than e are not shifted correctly.
>
> Has anybody programmed such calculation already and could give me a
> helping hint?

It might be easier to first treat the interval as being [0;e-b], and
then do the modulus (n-b)%(e-b). You should then be able to shift this