Fixed Point decimal complement one-digit multiplication (Booth comparison method)

Source: Internet
Author: User
Tags bitset

Program:

// Fixed point decimal complement one-digit multiplication (Booth comparison method) // http://blog.csdn.net/justme0#define _ crt_secure_no_warnings # include <iostream> # include <bitset> # include <string> using namespace STD; const int N = 4; // number of digits // a, B Union right shift (arithmetic shift) void rightmove (bitset <n + 2> &, bitset <n + 1> & B, bool & ebit) {ebit = B [0]; B> = 1; B [N] = A [0]; a> = 1; A [n + 1] = A [n]; // arithmetic right shift} bitset <n + 2> operator + (bitset <n + 2>, bitset <n + 2> B) // evaluate the arithmetic sum of A and B and {return. To_ullong () + B. to_ullong ();} bitset <n + 2> operator-(bitset <n + 2> A, bitset <n + 2> B) {return. to_ullong ()-B. to_ullong ();} bitset <n + 1> getcomplement (bitset <n + 1> A) {if (a [n]) {A = ~ A. to_ullong () + 1;. set (n); // note} return a;} bitset <2 * n + 1> getcomplement (const bitset <n + 2> high, const bitset <n + 1> LOW) // The low bitwise discard, because it does not belong to the product, but the symbol of the original multiplier {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 (&&! B) {return _ 10;} else // If (A & B) // all paths must return the value {return _ 11 ;}} bitset <2 * n + 1> complementonemul (const bitset <n + 1> X, const bitset <n + 1> Y) // transmits the bitset to the multiplier X and the multiplier y (original code representation) {bitset <n + 2> A; // A places partial product (the last is the high point of the product) bitset <n + 2> TMP = getcomplement (X ). to_ullong (); TMP [n + 1] = TMP [N]; // note the const bitset extension of the maximum bit of the complement Code <n + 2> B (TMP ); // B is the complement code of X. // you cannot use the different return values and overload them. Therefore, tmpbitset <n + 1> C = getcomplement (Y) is introduced. // C is y0.y1y2... YN (Y's complement) int CD = N + 1; // CD is the counter bool yn1 = 0; # pragma region core algorithm while (CD --) {Switch (test (C [0], yn1 )) // detect Y (I + 1)-y (I) {Case _ 00: Case _ 11: break; Case _ 10: A = A-B; break; case _ 01: A = a + B; break; default: break;} If (CD! = 0) // last non-shift {rightmove (A, C, yn1); // a, c union right shift, c down to yn1 }}# Pragma endregion core algorithm return getcomplement (a, c);} bitset <2 * n + 1> directmul (const bitset <n + 1> X, const bitset <n + 1> Y) {const bitset <n> X (X. to_ullong (); // returns the absolute value const bitset by truncating the high value. <n> Y (Y. to_ullong (); bitset <2 * n + 1> ans (X. to_ullong () * Y. to_ullong (); ans [2 * n] = x [N] ^ y [N]; // return ans;} int main (INT argc, char ** argv) {// freopen ("cin.txt", "r", stdin); string inputstrx; string inputstry; while (CIN> inputstrx> inputstry) {const bitset <n + 1> X (inputstrx); // X is the multiplier const bitset <n + 1> Y (inputstry ); // y is the cout multiplier <"complementonemul: \ t" <x <"*" <Y <"=" <complementonemul (x, y) <Endl; cout <"directmul: \ t" <x <"*" <Y <"=" <directmul (x, y) <Endl;} return 0 ;}

Running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.