divisioninteger-divisionforthcolor-forth# How does the colorForth /mod algorithm work?

I've been looking at Chuck Moore's colorForth recently, and I came upon this snippet of code (rendered in a traditional syntax):

```
: /mod for begin over over . + -if drop 2* [ swap ] next ; then over or or - 2* - next ;
```

With the following explanation:

`Divide operation: trial subtract and shift in either 0 or 1`

I'm really confused as to how this implements the full division operation. I realize the `2*`

shifts in a 0, the `- 2* -`

shifts in a 1, and `over or or`

implements a nip operation. I also understand the mixed loops and if combo.

Here's where I am falling short.

- It seems to be expecting two items on the stack, the numerator and the denominator, which makes sense. However, the initial
`for`

pushes the TOS to the return stack, leaving only one item on the return stack. The`over over`

operation works with two values present however, so I'm not sure what is happening. - He mentions subtraction, but there is no inversion happening, except for the
`- 2* -`

branch, which is already mentioned as shifting in a 1. - I'm not sure how you can construct the quotient bit by bit by only shifting in 1s or 0s (into the divisor?).

Some thoughts:

- Maybe it depends on the particular word size of the chip Chuck was programming and the rollover after adding enough times
- Maybe there is a preamble missing that inverts the denominator, resulting in the subtraction that is mentioned on every loop.

Some idiosyncrasies between colorForth and other Forths:

`.`

is a nop for timing purposes on Chuck's chips.`-`

is a bitwise inversion, rather than subtraction.`or`

is exclusive or instead of inclusive or

For additional information, Here's the source: Description of function and use of colorForth opcodes

Solution

Just for reference: the excellent answer on this question was posted in comp.lang.forth by Ulrich Hoffmann.

Please edit this post to make it more detailed.

- how to use division-class for image in html css?
- Do most compilers transform % 2 into bit comparison? Is it really faster?
- While looping a range of integers, convert integer representing number of months to the number of years and months
- Fastest way to check if a number is divisible by another in python
- In Python, what is a good way to round towards zero in integer division?
- MOVZX and CWD - Are they interchangeable?
- Truncating division in Python 3.2
- numeric column operation in r with condition from another factor level
- Java unsigned division without casting to long?
- Extracting summary from selected records as a division of values
- This is a currency converter app . Why cant i divide it?
- How can I extend my code do divide by a 64bit integer? (128bit / 64bit)
- uint64_t: Division with rounding
- Why float division is faster than integer division in c++?
- Division By Zero Error In Laravel Web App
- Does division take longer when dividing perfect factors?
- pandas columns division returns multiple columns
- Inverse of fmod
- cannot convert from double to float
- How does the colorForth /mod algorithm work?
- Kotlin: FInd closest divisible
- Scala division result 0
- How to divide an odd number to get x integers?
- How can I calculate divide and modulo for integers in C#?
- How can I multiply and divide using only bit shifting and adding?
- Make elements with value division by zero equal to zero in a 2D numpy array
- Divide a number by 3 without using *, /, +, -, % operators
- Is it possible to get the unsigned quotient and remainer at once in C?
- Multiply a fraction by 100 after div, to get the first two decimal places
- Precision problem when dividing Double values in Haskell