mawinkle 6 年之前
父节点
当前提交
9441a20fc0
共有 2 个文件被更改,包括 47 次插入3 次删除
  1. 42 1
      BigInt.hpp
  2. 5 2
      test.cpp

+ 42 - 1
BigInt.hpp

@@ -1,7 +1,8 @@
 #ifndef BIG_INT_HPP
 #define BIG_INT_HPP
 #include "semi_bitset.hpp"
-template<std::size_t size>
+#include <cstdlib>
+template<size_t size>
 class BigInt{
 private:
 	int signum = 1;
@@ -26,6 +27,46 @@ public:
 		bits.data[bits.length() - 1] = (o & lower_half);
 		bits.data[bits.length() - 2] = (o >> 32);
 	}
+	template<std::size_t osize>
+	BigInt<size> add(const BigInt<osize>& o)const{
+		static_assert(size == osize);
+		BigInt<size> ret;
+		uint64_t carry = 0;
+		for(size_t i = size - 1;i >= 0;i--){
+			uint64_t res = (*this)[i] + o[i] + carry;
+			carry = (res >> 32);
+			ret[i] = res & lower_half;
+			if(i == 0)break;
+		}
+		return ret;
+	}
+	
+	BigInt<size> div(unsigned int o)const{
+		uint64_t carry = 0;
+		BigInt<size> ret;
+		for(size_t i = 0;i < size;i++){
+			lldiv_t dm = std::div((unsigned long long int)((carry << 32) + (*this)[i]),(unsigned long long int)o);
+			carry = dm.rem;
+			std::cout << " " << dm.rem << " ";
+			ret[i] = dm.quot;
+		}
+		return ret;
+	}
+	unsigned int mod(unsigned int o)const{
+		uint64_t carry = 0;
+		for(size_t i = 0;i < size;i++){
+			lldiv_t dm = std::div((long long int)((carry << 32) + (*this)[i]),(unsigned long long int)o);
+			carry = dm.rem;
+		}
+		return (unsigned int)(carry & lower_half);
+	}
+	std::uint64_t& operator[](size_t i){
+		return bits.data[i];
+	}
+	
+	const std::uint64_t& operator[](size_t i)const{
+		return bits.data[i];
+	}
 	template<typename stream, size_t osize>
 	inline friend stream& operator<<(stream& s, const BigInt<osize>& o){
 		for(size_t i = 0;i < o.bits.length();i++)

+ 5 - 2
test.cpp

@@ -21,8 +21,11 @@ std::ostream& operator<< <char>(std::ostream& out, std::vector<char> o){
 	return out;
 }
 int main(){
-	BigInt<2> a = (1ULL << 35);
-	std::cout << a << "\n";
+	BigInt<2> a = (1LL << 63);
+	while(a[1]){
+		std::cout << a.mod(10);
+		a = a.div(10);
+	}
 }
 int mian(){
 	cppsocket sock("192.168.178.79", 80);