aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bigint.cpp21
-rw-r--r--bigint.h5
2 files changed, 13 insertions, 13 deletions
diff --git a/bigint.cpp b/bigint.cpp
index 0827e1e..1f71cf9 100644
--- a/bigint.cpp
+++ b/bigint.cpp
@@ -523,28 +523,27 @@ bool Bigint::odd() const {
return !even();
}
-vector<char> Bigint::serialise() const {
- vector<char> v(1+digits.size()*sizeof(digit_t));
- v[0]=sign;
+string Bigint::serialiseMantissa() const {
+ string s;
+ s.resize(digits.size()*sizeof(digit_t));
int sz=digits.size();
for(int i=0;i<sz;i++){
for(int j=0;j<(int)sizeof(digit_t);j++){
- v[1+i*sizeof(digit_t)+j]=(digits[i]>>(8*j))&255;
+ s[i*sizeof(digit_t)+j]=(digits[i]>>(8*j))&255;
}
}
- return v;
+ return s;
}
-void Bigint::deserialise(const vector<char> &v){
- assert(v.size()%4==1);
- sign=(int)v[0];
- assert(sign==1||sign==-1);
- int sz=v.size()/4;
+void Bigint::deserialiseMantissa(const string &s){
+ assert(s.size()%4==0);
+ sign=1;
+ int sz=s.size()/4;
digits.resize(sz);
for(int i=0;i<sz;i++){
digits[i]=0;
for(int j=0;j<(int)sizeof(digit_t);j++){
- digits[i]|=v[1+i*sizeof(digit_t)+j]<<(8*j);
+ digits[i]|=s[i*sizeof(digit_t)+j]<<(8*j);
}
}
shrink();
diff --git a/bigint.h b/bigint.h
index f5f3f17..24f0945 100644
--- a/bigint.h
+++ b/bigint.h
@@ -1,6 +1,7 @@
#pragma once
#include <iostream>
+#include <string>
#include <vector>
#include <utility>
#include <cstdint>
@@ -86,8 +87,8 @@ public:
bool even() const;
bool odd() const;
- std::vector<char> serialise() const;
- void deserialise(const std::vector<char>&);
+ std::string serialiseMantissa() const; //stores everything but the sign
+ void deserialiseMantissa(const std::string&); //restores non-negative number
std::vector<bool> bits() const;
friend std::istream& operator>>(std::istream&,Bigint&);