• Skip to main content
  • Skip to header right navigation
  • Skip to site footer
Retro Game Coders

Retro Game Coders

Retro computer/console game + dev community

  • About
    • Retro Computer Collection
    • Contact
  • Blog
  • Retro Resources
    • Retro Gaming Timeline
    • Online Retro IDE
    • Retro Pixel Art Editor
    • Dungeon Loom Map Editor
    • 6502 Programmer’s Reference
    • Emulators
      • Acorn Electron
      • Amstrad CPC Emulator
      • Online BBC Micro Emulator
      • Commodore PET Emulator
      • Browser C64 Emulator
      • DOSBox/DOS PC emulator
      • Tandy CoCo/Dragon
    • Best Retro YouTube Channels
    • New Retro Books
    • Raspberry Pi Amiga Emulation
    • MiSTer FPGA Tutorial
    • BMC64 C64 Pi
  • Community

Home » Retro Game Coders Blog » Programming

How to Modulo on the C64 and Why You Need It

Modulo on the C64

Modern systems have an extremely useful modulo operator, but in BASIC V2 on the C64 there isn’t one. Here’s how we can make our own.

If you write games or graphical demos on the C64, you will sooner or later need a way to check a number is odd or even, wrap numbers around, step through repeating patterns, or check if something should happen every N frames.

On modern systems you would reach for the modulo operator, but in BASIC V2 on the C64 there isn’t one. That means we need to build it ourselves.

Let’s explore what modulo is, why it matters, and how to recreate it reliably in BASIC …

Join Retro Game Coders Community
Join the Retro Game Coders Community

What modulo is

Modulo gives the remainder after dividing one whole number by another.

Example:

10 / 3 = 3 remainder 1

This remainder is what modulo returns.

If you have ever:

  • looped back to the first animation frame
  • wrapped a sprite’s position around the edge of the screen
  • repeated a pattern every N characters
  • checked for odd/even
  • spaced items evenly across the screen

…then you have likely relied on the behaviour that modulo provides.

You can now follow the tutorials and edit the code right in your web browser with the Online Retro IDE

– No downloads, configuration, etc necessary, and it is free!

Fractional vs Remainders

I am not great at math so when I say this is a common mistake, perhaps it is just a mistake I commonly make. Regardless, when you divide numbers in C64 BASIC you get a floating point value.

That means:

10 / 3 = 3.333333

Some people, like me, would then subtract the integer part and think the fractional part is the remainder:

3.333333 - 3 = .333333   (wrong)

The fractional part is a proportion. For modulo to be useful in the scenarios mentioned earlier, we need an integer remainder:

10 = 3*3 + 1   (correct == remainder)

So we need to compute that remainder ourselves.

How to Modulo in C64 BASIC V2

This is the way:

remainder = V - (INT(V/D) * D)

This strips off the fractional part, multiplies back up, and leaves the remainder.

Insert this anywhere you need proper modulo behaviour.

💬 Questions or comments? Head over to the community to discuss!

Demo

Here is a simple snippet you can use to see it in action:

10 REM C64 IMPLEMENTATION OF MODULO
20 V=0: D=0: POKE 53280,0: POKE 53281,0: LB$=CHR$(154): W$=CHR$(5)
30 PRINT CHR$(147)
50 PRINT LB$
60 INPUT "ENTER VALUE";V
70 INPUT "ENTER NUMBER TO DIVIDE BY";D
80 IF D=0 THEN PRINT "CANNOT DIVIDE BY ZERO":GOTO 50
90 PRINT "DIVIDES EQUALLY? ";
100 IF V/D=INT(V/D) THEN PRINT W$"TRUE": REM DOES DIVIDE EQUALLY
110 IF V/D<>INT(V/D) THEN PRINT W$"FALSE": REM DOES NOT DIVIDE EQUALLY
120 PRINT LB$"REMAINDER: "W$V-(INT(V/D)*D): REM REMAINDER
130 PRINT LB$V" IS ODD OR EVEN? ";
140 IF V AND 1 THEN PRINT W$"ODD"
150 IF (V AND 1) =0 THEN PRINT W$"EVEN"
160 GOTO 50

Now let’s put modulo to work in a game-style example.

Example: drawing repeating stripes with reverse video

The C64 screen is 40 characters wide. Suppose you want to draw vertical stripes that alternate every four columns. This is a classic modulo job: each column number repeats in a cycle of 0 to 3, no matter how far across the screen you go.

Ordinarily we would do something like:

column MOD 4

to decide which columns get reverse video.

Here is how we can do it on the C64:

PRINT CHR$(147)
FOR Y=0 TO 24
  FOR X=0 TO 39
    R = X - (INT(X/4)*4)   : REM X MOD 4
    IF R=0 OR R=1 THEN PRINT "{RVS ON} "; : GOTO NEXTCOL
    PRINT "{RVS OFF} ";
NEXTCOL:
  NEXT X
  PRINT
NEXT Y
PRINT "{RVS OFF}"

What this does:

  • each column number is reduced to 0,1,2,3 using the modulo technique
  • columns where the result is 0 or 1 print in reverse
  • columns where the result is 2 or 3 print normally
  • the pattern repeats every four columns across the full width

Chequerboard Pattern with Modulo

Now that you’ve seen how modulo can repeat a pattern across the screen, you can combine the column number and line number to create more interesting effects. A classic example is the chequerboard: alternating squares of reverse and normal video.

The idea is simple:

  • alternate every N columns
  • alternate every N rows
  • when both conditions line up, you flip the colour or style

Modulo gives you exactly the control you need over those repeating intervals.

For a basic 2×2 pattern, you want to repeat every 2 columns and every 2 rows.

That means:

X MOD 2
Y MOD 2

If the sum of those two results is even, draw one style.
If it is odd, draw the other.

This is a common trick in tile-based games.

Here is a full working snippet using the same modulo approach as before:

10 RV$=CHR$(18): REM REVERSE 
20 RO$=CHR$(146): REM REVERSE OFF
30 PRINT CHR$(147);
40 FOR Y=0 TO 24
50 FOR X=0 TO 39
60 RX = X - (INT(X/2)*2) :REM X MOD 2
70 RY = Y - (INT(Y/2)*2) :REM Y MOD 2
80 C = RX + RY
90 IF C=0 OR C=2 THEN PRINT RV$" "; :GOTO 120
100 PRINT RO$" ";
120 NEXT X
130 NEXT Y
140 PRINT RO$

What it does

  • RX cycles between 0 and 1 as X increases
  • RY cycles between 0 and 1 as Y increases
  • adding them together produces a repeating 0–1–1–2 pattern
  • values 0 and 2 produce one style
  • values 1 and 1 produce the other

This gives a clean, repeating chequerboard across the entire 40×25 screen.

Extending the pattern

To make the squares larger, change the divisor:

  • X MOD 4 and Y MOD 4 gives 4×4 blocks
  • X MOD 8 and Y MOD 8 gives 8×8 blocks
  • mix sizes for offset effects

Binary Trick

An even more efficient option exists if you simply want odd/even:

PRINT "{LIGHTBLUE}"V" IS ODD OR EVEN? ";
IF V AND 1 THEN PRINT "{WHITE}ODD"
IF (V AND 1)=0 THEN PRINT "{WHITE}EVEN"

This checks the first bit and returns the result, using the native language of computers – binary!

Conclusion

C64 BASIC might lack a built-in modulo operator, but with the simple remainder formula you can build all the repeating structures you need for game backgrounds, tile maps, sprite cycling, and animation patterns.

Category: ProgrammingTag: basic programming, Commodore 64 (C64)
Previous Post:C64 BASIC OPTIMISATIONMore C64 BASIC Optimisations
Next Post:Creating a Classic 3D Maze Game for the C64 in XC-BASIC 3Classic 3D Maze Game

Retro Game Coders

Retro computer/console game + dev programming community by Chris Garrett

  • Facebook
  • Twitter
  • Instagram
  • YouTube

Maker Hacks ・ D6Combat・chrisg.com

© Copyright 2025 Chris Garrett

Privacy ﹒ Terms of Service

Return to top