程序:
[cpp]
// 定点小数补码一位乘(Booth比较法)
// http://blog.csdn.net/justme0
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <bitset>
#include <string>
using namespace std;
const int n = 4; // 数值位位数
// a,b联合右移(算术移位)
void RightMove(bitset<n + 2> &a, bitset<n + 1> &b, bool &eBit)
{
eBit = b[0];
b >>= 1;
b[n] = a[0];
a >>= 1;
a[n + 1] = a[n]; // 算术右移
}
bitset<n + 2> operator+(bitset<n + 2> a, bitset<n + 2> b) // 求a,b的算术和
{
return a.to_ullong() + b.to_ullong();
}
bitset<n + 2> operator-(bitset<n + 2> a, bitset<n + 2> b)
{
return a.to_ullong() - b.to_ullong();
}
bitset<n + 1> GetComplement(bitset<n + 1> a)
{
if (a[n])
{
a = ~a.to_ullong() + 1;
a.set(n); // NOTE
}
return a;
}
bitset<2 * n + 1> GetComplement(const bitset<n + 2> high, const bitset<n + 1> low) // low的最低位舍弃,因为它不属于积,而是原来乘数的符号
{
bitset<2 * n + 1> ans(high.to_string().substr(1) + low.to_string().substr(0, 4));
if (ans[2 * n])
{
ans = ~ans.to_ullong() + 1;
ans.set(2 * n); // NOTE
}
return ans;
}
enum Sign{_00, _01, _10, _11};
Sign Test(bool a, bool b)
{
if (!a && !b)
{
return _00;
}
else if (!a && b)
{
return _01;
}
else if (a && !b)
{
return _10;
}
else // if (a && b) // 所有路径都必须返回值
{
return _11;
}
}
bitset<2 * n + 1> ComplementOneMul(const bitset<n + 1> X, const bitset<n + 1> Y)//传进被乘数X和乘数Y(原码表示)