Nov
4
mybits - 自己实现的"位桶"
这两天在看密码学的大作业,写一个DES加密程序,里面有很多涉及位运算的东西。
虽然直接用bitset写,但是非常非常地不顺手,于是自己写了一个类,里头有DES需要的运算,恩。
如果谁发现有错的话,麻烦告诉我一声。
好久没有写这样的代码了,一个简单的类居然花了这么久。囧。
转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php 。
虽然直接用bitset写,但是非常非常地不顺手,于是自己写了一个类,里头有DES需要的运算,恩。
如果谁发现有错的话,麻烦告诉我一声。
好久没有写这样的代码了,一个简单的类居然花了这么久。囧。
#include<iostream>
#include<cstdlib>
using namespace std;
class mybits{
private:
int n;
int *d;
public:
mybits(){n = 0; d = NULL;}
mybits(int _n){
n = _n;
if(n <= 0) { cout << "n <= 0!" << endl; exit(1); }
d = new int[n];
for(int i = 0; i < n; i++) d[i] = 0;
}
mybits(string t){
n = t.length();
if(n <= 0) { cout << "n <= 0!" << endl; exit(1); }
d = new int[n];
for(int i = 0; i < n; i++) d[i] = (t[i] == '1') ? 1 : 0;
}
mybits(int _n, string t){
int i;
n = _n;
if(n <= 0) { cout << "n <= 0!" << endl; exit(1); }
d = new int[n];
for(i = 0; i < n && (unsigned int)i < t.length(); i++) d[i]=(t[i]=='1')?1:0;
while(i < n) d[i++] = 0;
}
mybits(mybits &b){
n = b.n;
d = new int[n];
for(int i = 0; i < n; d[i] = b.d[i], i++);
}
string to_string(){
string t;
for(int i = 0; i < n; i++) t += char(d[i]+'0');
return t;
}
friend inline ostream & operator <<(ostream &os, mybits &a){
os << a.to_string();
return os;
}
// 长度
int length() {return n;}
// 访问
int & operator [] (int i){
if(i >= n || i < 0){ cout << "[] OUT OF RANGE" << endl; exit(1); }
return d[i];
}
// 赋值
mybits & operator = (const mybits &b){
if(n == 0){ n = b.n; d = new int[n];}
if(n != b.n){ cout << "= NOT MATCH" << endl; exit(1); }
for(int i = 0; i < n; i++) d[i] = b.d[i];
return (*this);
}
// 异或
mybits & operator ^= (const mybits & b){
if(n != b.n){ cout << "^= NOT MATCH" << endl; exit(1); }
for(int i = 0; i < n; ++i) d[i] ^= b.d[i];
return (*this);
}
// 左移
mybits & operator <<= (int t){
if(n <= 0) {cout << "<<= Init first!" << endl; exit(1);}
t %= n;
int i = 0, j = t;
while(j < n) d[i++] = d[j++];
while(i < n) d[i++] = 0;
return (*this);
}
friend inline mybits operator << (mybits &a, int t){
mybits tmp(a.n);
int i = 0, j = t;
t %= a.n;
while(j < a.n) tmp.d[i++] = a.d[j++];
return tmp;
}
// 右移
mybits & operator >>= (int t){
if(n <= 0) {cout << ">>= Init first!" << endl; exit(1);}
t %= n;
int i = n - 1, j = n - 1 - t;
while(j >= 0) d[i--] = d[j--];
while(i >= 0) d[i--] = 0;
return (*this);
}
friend inline mybits operator >> (mybits &a, int t){
mybits tmp(a.n);
int i = 0, j = t;
t %= a.n;
while(j < a.n) tmp.d[j++] = a.d[i++];
return tmp;
}
// 或
friend inline mybits operator | (const mybits &a, const mybits &b){
if(a.n != b.n) {cout << "| NOT MATCH" << endl; exit(1); }
mybits t(a.n);
for(int i = 0; i < a.n; i++) t.d[i] = a.d[i] | b.d[i];
return t;
}
// 异或
friend inline mybits operator ^ (const mybits &a, const mybits &b){
if(a.n != b.n) {cout << "^ NOT MATCH" << endl; exit(1); }
mybits t(a.n);
for(int i = 0; i < a.n; i++) t.d[i] = a.d[i] ^ b.d[i];
return t;
}
// 拼接
friend inline mybits operator + (const mybits &a, const mybits &b){
mybits t(a.n + b.n);
int i, j = 0;
for(i = 0; i < a.n; i++) t.d[j++] = a.d[i];
for(i = 0; i < b.n; i++) t.d[j++] = b.d[i];
return t;
}
};
int main(){
mybits a("11110000"), b, c;
//循环左移3位(其实应该专门写一个方法优化的,偷懒了)
b = (a >> 5) | (a << 3);
cout << b << endl;
//拼接
c = a + b;
cout << c << endl;
return 0;
}
#include<cstdlib>
using namespace std;
class mybits{
private:
int n;
int *d;
public:
mybits(){n = 0; d = NULL;}
mybits(int _n){
n = _n;
if(n <= 0) { cout << "n <= 0!" << endl; exit(1); }
d = new int[n];
for(int i = 0; i < n; i++) d[i] = 0;
}
mybits(string t){
n = t.length();
if(n <= 0) { cout << "n <= 0!" << endl; exit(1); }
d = new int[n];
for(int i = 0; i < n; i++) d[i] = (t[i] == '1') ? 1 : 0;
}
mybits(int _n, string t){
int i;
n = _n;
if(n <= 0) { cout << "n <= 0!" << endl; exit(1); }
d = new int[n];
for(i = 0; i < n && (unsigned int)i < t.length(); i++) d[i]=(t[i]=='1')?1:0;
while(i < n) d[i++] = 0;
}
mybits(mybits &b){
n = b.n;
d = new int[n];
for(int i = 0; i < n; d[i] = b.d[i], i++);
}
string to_string(){
string t;
for(int i = 0; i < n; i++) t += char(d[i]+'0');
return t;
}
friend inline ostream & operator <<(ostream &os, mybits &a){
os << a.to_string();
return os;
}
// 长度
int length() {return n;}
// 访问
int & operator [] (int i){
if(i >= n || i < 0){ cout << "[] OUT OF RANGE" << endl; exit(1); }
return d[i];
}
// 赋值
mybits & operator = (const mybits &b){
if(n == 0){ n = b.n; d = new int[n];}
if(n != b.n){ cout << "= NOT MATCH" << endl; exit(1); }
for(int i = 0; i < n; i++) d[i] = b.d[i];
return (*this);
}
// 异或
mybits & operator ^= (const mybits & b){
if(n != b.n){ cout << "^= NOT MATCH" << endl; exit(1); }
for(int i = 0; i < n; ++i) d[i] ^= b.d[i];
return (*this);
}
// 左移
mybits & operator <<= (int t){
if(n <= 0) {cout << "<<= Init first!" << endl; exit(1);}
t %= n;
int i = 0, j = t;
while(j < n) d[i++] = d[j++];
while(i < n) d[i++] = 0;
return (*this);
}
friend inline mybits operator << (mybits &a, int t){
mybits tmp(a.n);
int i = 0, j = t;
t %= a.n;
while(j < a.n) tmp.d[i++] = a.d[j++];
return tmp;
}
// 右移
mybits & operator >>= (int t){
if(n <= 0) {cout << ">>= Init first!" << endl; exit(1);}
t %= n;
int i = n - 1, j = n - 1 - t;
while(j >= 0) d[i--] = d[j--];
while(i >= 0) d[i--] = 0;
return (*this);
}
friend inline mybits operator >> (mybits &a, int t){
mybits tmp(a.n);
int i = 0, j = t;
t %= a.n;
while(j < a.n) tmp.d[j++] = a.d[i++];
return tmp;
}
// 或
friend inline mybits operator | (const mybits &a, const mybits &b){
if(a.n != b.n) {cout << "| NOT MATCH" << endl; exit(1); }
mybits t(a.n);
for(int i = 0; i < a.n; i++) t.d[i] = a.d[i] | b.d[i];
return t;
}
// 异或
friend inline mybits operator ^ (const mybits &a, const mybits &b){
if(a.n != b.n) {cout << "^ NOT MATCH" << endl; exit(1); }
mybits t(a.n);
for(int i = 0; i < a.n; i++) t.d[i] = a.d[i] ^ b.d[i];
return t;
}
// 拼接
friend inline mybits operator + (const mybits &a, const mybits &b){
mybits t(a.n + b.n);
int i, j = 0;
for(i = 0; i < a.n; i++) t.d[j++] = a.d[i];
for(i = 0; i < b.n; i++) t.d[j++] = b.d[i];
return t;
}
};
int main(){
mybits a("11110000"), b, c;
//循环左移3位(其实应该专门写一个方法优化的,偷懒了)
b = (a >> 5) | (a << 3);
cout << b << endl;
//拼接
c = a + b;
cout << c << endl;
return 0;
}
欢迎扫码关注:
转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php 。