Skip to content

Instantly share code, notes, and snippets.

View ggcrunchy's full-sized avatar

Steven Johnson ggcrunchy

View GitHub Profile
@ggcrunchy
ggcrunchy / main.lua
Created January 24, 2020 18:11
Integer division using "bitwise ops", long division approach instead of iterated subtraction
-- Tested on repl.it
local N = 8 -- or 16, etc. Could also adapt for fixed point
local lshift = math.ldexp
local HighBitMask = lshift(1, N - 1)
local function Div (a, b)
local acc, result = 0, 0
local mask = HighBitMask -- one-hot mask