2014-09-27 39 views
1

我有這個功能,一切工作,直到a和b都等於1.當發生這種情況時,進位是2,當它應該是隻有0或1.我的算法是否有進位錯誤
carry_bits | =((a &掩碼)​​&(b &掩碼)​​)< < 1u;在我的7位加法器進位是不正確的,我不知道爲什麼

bool add7bits(unsigned int a, unsigned int b, unsigned int *carry_out, unsigned int  *overflow_out, unsigned int *sum_out) 
{ 
if (a > ((1u << NUM_BITS) - 1)) 
{ 
    cout << "a value: " << a << " is too large" << endl; 
    return false; 
} 
else if (b > ((1u << NUM_BITS) - 1)) 
{ 
    cout << "b value: " << b << " is too large" << endl; 
    return false; 
} 
else 
{ 
    unsigned int sum_bits = 0; 
    unsigned int carry_bits = 0; 
    // Use a mask to access the specific bits of interest 
    unsigned int mask = 1u; 

    // Handle rightmost bit as a half-adder (no carry input) 
    // sum = a^b 
    // c_out = a & b 
    sum_bits |= (a & mask)^(b & mask); 

    // The carry _out_ from this stage sets the carry in for the next, 
    // that is, the next higher bit in the carry_bits value 

    carry_bits |= ((a & mask) & (b & mask)) << 1u; 

    // The remaining bits must be handled with the full adder logic. The last 
    // adder in the chain's carry out becomes the carry output return 
    // value of this function. 
      mask = 1u << 1; 

      sum_bits |= ((a & mask)^(b & mask)^carry_bits); 
      carry_bits |= ((a & mask) & (b & mask)) << 1u; 

      mask = 1u << 2; 
      sum_bits |= ((a & mask)^(b & mask)^carry_bits); 
      carry_bits |= ((a & mask) & (b & mask)) << 1u; 

      mask = 1u << 3; 
      sum_bits |= ((a & mask)^(b & mask)^carry_bits); 
      carry_bits |= ((a & mask) & (b & mask)) << 1u; 

      mask = 1u << 4; 
      sum_bits |= ((a & mask)^(b & mask)^carry_bits); 
      carry_bits |= ((a & mask) & (b & mask)) << 1u;   

      mask = 1u << 5; 
      sum_bits |= ((a & mask)^(b & mask)^carry_bits); 
      carry_bits |= ((a & mask) & (b & mask)) << 1u; 
    // Handle bit 6 separately. 
    mask = 1u << 6; 
    sum_bits = ((a ^b)^carry_bits); 
    carry_bits |= ((a & mask) & (b & mask)) << 1u; 


// Determine the overflow by checking if a and b are both 1. 
    bool overflow = false; 
    if ((a & mask) & (b & mask)) 
     overflow = true; 
    // ... 
    // 
*sum_out = sum_bits; 
*carry_out = carry_bits; 
*overflow_out = overflow; 
cout << a << " + " << b << " = " << *sum_out << endl;; 
cout << "Carry: " << *carry_out << endl; 
cout << "Overflow: " << *overflow_out << endl; 
    return true; 
} 
} 

回答

0

你或-ING在所有的前一進位到總和位每個階段,而不僅僅是當前階段的進位。你想

sum_bits |= ((a & mask)^(b & mask)^(carry_bits & mask)); 

在每一個階段,或等效的,更簡單:

sum_bits |= (a^b^carry_bits) & mask; 

你進位輸出計算是不正確 - 它沒有考慮在攜帶進去。你想:

carry_bits |= ((a & b | a & carry_bits | b & carry_bits) & mask) << 1; 

你最後的進位出位carry_bits的第8位,所以你想:

*carry_out = carry_bits >> NUM_BITS; 

寫這一點位。

「溢出」在這裏沒有什麼意義,因爲你似乎在做無符號加法(所以「溢出」到它存在的程度,與執行相同)。如果你正在做的二進制補簽署此外,你想要的最後兩個進位的XOR:

*overflow_out = ((carry_out >> NUM_BITS)^(carry_out >> (NUM_BITS-1))) & 1; 
+0

非常感謝!這非常有幫助!我知道我的溢出仍然需要工作。我只是想弄清楚運載工具應該如何工作。你的解釋太棒了! – user2948878 2014-09-28 04:19:35

1

進位是1,這是在第二位置,所以carry_bits...0010,或2

+0

如何我會得到它轉移到0001 – user2948878 2014-09-27 17:31:56

+0

@ user2948878:爲什麼你要?如果你想把這個進位位保存爲一個BOOL,把它作爲BOOL存儲。 – Beta 2014-09-27 17:40:53

+0

我希望我的* carry_out是0或1 ....我不希望它是2. – user2948878 2014-09-27 17:46:07

相關問題