Skip to content

Instantly share code, notes, and snippets.

@ldematte
Created January 19, 2013 18:41
Show Gist options
  • Save ldematte/4574228 to your computer and use it in GitHub Desktop.
Save ldematte/4574228 to your computer and use it in GitHub Desktop.
Sequential algorithm for integer subdivision, implemented at bit level. Part of the material for a course on Computer Architecture.
#include <bitset>
using std::bitset;
bitset<32> division(bitset<32> dividend, bitset<32> divisor)
{
bitset<32> result(0);
bitset<32> now(0);
int n = 31;
while (n >= 0)
{
bool first_translation = true;
while (n >= 0)
{
now <<= 1;
now[0] = dividend[n];
--n;
if (now.to_ulong() <= divisor.to_ulong())
result <<= 1;
else
break;
first_translation = false;
}
bitset<32> reminder(now.to_ulong() - divisor.to_ulong());
result <<= 1;
result[0] = 1;
now = reminder;
}
return result;
}
int main()
{
bitset<32> result = division(bitset<32>(365), bitset<32>(52));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment