Francois Berder / BigInt
Revision:
7:1aad58757705
Parent:
6:29e78b169f40
Child:
8:2a361f1b41da
--- a/BigInt.cpp	Fri Oct 04 14:39:15 2013 +0000
+++ b/BigInt.cpp	Fri Oct 04 15:10:31 2013 +0000
@@ -413,6 +413,66 @@
     return a.bits[0] == 0;
 }
  
+ 
+BigInt operator&(const BigInt &a, const BigInt &b)
+{
+    BigInt result;
+
+    result.size = a.size < b.size ? a.size : b.size;
+    uint32_t l = num(result.size);
+    result.bits = new uint32_t[l];
+    memset(result.bits, 0, l);
+    
+    for(uint32_t i = 0; i < l; ++i)
+        result.bits[i] = a.bits[i] & b.bits[i];
+
+    
+    // trim result
+    uint8_t *tmp = (uint8_t*)result.bits;
+    uint32_t newSize = result.size;
+    while(tmp[newSize-1] == 0 && newSize > 0)
+        newSize--;
+    if(num(newSize) < num(result.size))
+        result.bits = (uint32_t*)realloc(result.bits, sizeof(uint32_t)*num(newSize)); 
+    result.size = newSize; 
+    
+    return result;
+}
+
+BigInt& BigInt::operator&=(const BigInt &a)
+{
+    return (*this = *this & a);
+}
+
+BigInt operator|(const BigInt &a, const BigInt &b)
+{
+    BigInt result;
+
+    uint32_t na = num(a.size);
+    uint32_t nb = num(b.size);
+    uint32_t l = max(na,nb);
+    result.size = max(a.size, b.size);
+    result.bits = new uint32_t[l];
+    memset(result.bits, 0, l);
+    
+    for(uint32_t i = 0; i < l; ++i)
+    {
+        if(i < na && i < nb)
+            result.bits[i] = a.bits[i] | b.bits[i];
+        else if(i < na)
+            result.bits[i] = a.bits[i];
+        else
+            result.bits[i] = b.bits[i];
+    }
+    
+    return result;
+}
+
+BigInt& BigInt::operator|=(const BigInt &a)
+{
+    return (*this = *this | a);
+}
+    
 void BigInt::print()
 {
     printf("size: %d bytes\n", size);