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.

- Regular Expression for Binary Numbers Divisible by 5
- Delphi 6 Compiler Options (Pentium-safe FDIV)
- (a * b) / c MulDiv and dealing with overflow from intermediate multiplication
- Is there a language that defines remainder modulo zero to equal the dividend? Why is it not common to define it that way?
- Why does Clang do this optimization trick only from Sandy Bridge onward?
- Division with negative dividend, but rounded towards negative infinity?
- Why byte and short division results in int in Java?
- Python: Remove division decimal
- Working with 64-bit products and quotients of 32-bit integers in assembly x86-64
- Cobol - checking for remainder in IF clause
- how can i get point from user's input amount in php
- which operation takes more CPU clocks, modulo or comparison?
- Division Round Up in C++
- How to test if a BigDecimal object is divisible by another BigDecimal in Java?
- Divmod algorithm in brainfuck
- Integer division in awk
- Dividing large number by 10 in python gives the wrong result
- Why does division (3/5) result in zero despite being stored as a double?
- Handling division of double by a large power of 2 in C++
- Create an array of div_t type structure elements in C++
- Euclidean algorithm
- How to check the Rest in a Floating point division in MIPS?
- Calculating simple returns (one cell divided by cell above it in same column, result in another column) across large sets of data, google sheets
- How to change color of divisions in Slider flutter
- How to quickly tell if an "unknown" number is divisible by 3?
- Algorithm to split integer into groups of defined smaller integers
- tensor division in pytorch. Assertion error
- Why does integer division result in zero instead of a decimal?
- Dividing 1 by any number gives 0
- Why don't x86-64 (or other architectures) implement division by 10?