rem invert.bas

rem Now works even in modes without numeric logical operators.

d= sysvarptr
charset= peek32 (d + 20)

def fn mirror (a)
	local i
	mirror= 0
	for i= 1 to 8
		mirror= mirror * 2
		if a mod 2 <> 0 then mirror= mirror + 1
		a= a \ 2
	next
fn end

def fn invert (a)
	local i, n
	invert= 0
	n= 1
	for i= 1 to 8
		if a mod 2 = 0 then invert= invert + n
		n= n * 2
		a= a \ 2
	next
fn end

if peek16 (sysvarptr) = 0 then mode 10

print "Inverse": print

gosub init_invert

gosub invert
list
print
gosub invert
get a$

print "Mirror": print

gosub init_mirror

gosub mirrorize
list
print
gosub mirrorize
get a$

print "Inverse & mirror": print

gosub invert
gosub mirrorize
list
print
gosub invert
gosub mirrorize
get a$

end

label init_invert

dim inverttable (255)

for i= 0 to 255
	inverttable (i)= fn invert (i)
next

return

label invert

for i= charset + asc ("!") * 8 to charset + 256 * 8 - 1
	poke i, inverttable (peek (i) )
next

return

label init_mirror

dim mirrortable (255)

for i= 0 to 255
	mirrortable (i)= fn mirror (i)
next

return

label mirrorize

for i= charset + asc ("!") * 8 to charset + 256 * 8 - 1
	poke i, mirrortable (peek (i) )
next

return
