#include <algorithm> #include <vector> #include <iostream.h> #include <string.h> #include <conio.h> typedef int element; class TabBit { public: TabBit(); void PokazCalosc(); int PobierzRozmiar(); bool set(bool zerojeden, int pozycja); bool get(int pozycja); vector<int> v1; private: int rozmiar; int maska; }; TabBit::TabBit() { this->rozmiar = 24; for(int i=0; i<=this->rozmiar-1; i++) { this->v1.push_back(0); } } bool TabBit::set(bool zerojeden, int pozycja) { // wstawienie pod dana pozycje jedynke lub zero //cout << endl << "Ustawienie bitu " << pozycja << " na " << zerojeden << endl; int nrbytu = pozycja / this->rozmiar; int nrbitu = pozycja % this->rozmiar; this->maska = 1<<nrbitu; int ktory = (nrbytu+nrbitu); //cout<<"ktory: " << ktory << " e: "<< this->v1[ktory]; if(zerojeden) this->v1[ktory] = this->v1[ktory] | this->maska; else this->v1[ktory] = this->v1[ktory] &~ this->maska; //cout<<" e2: "<< this->v1[ktory]; return true; } void TabBit::PokazCalosc() { cout << endl << "Zawartosc: "; for(int i=this->rozmiar; i>0; i--) { bool tmp = this->v1[i]; cout << tmp; if((i%8)==1) cout << " "; } cout << endl; } int TabBit::PobierzRozmiar() { return this->v1.size(); } bool TabBit::get(int pozycja) { return this->v1[pozycja]; } TabBit operator & (TabBit t1, TabBit t2) { TabBit temp; bool tmpv1, tmpv2; bool tmppush=0; for(int i=0; i<t1.PobierzRozmiar(); i++) { tmpv1 = t1.v1[i]; tmpv2 = t2.v1[i]; tmppush = tmpv1&tmpv2; //cout << "\nrozm: " << t1.PobierzRozmiar() << " i: " << i << " v[i]: " << tmpv1; //cout << "\ntmpv1: " << tmpv1 << " tmpv2: " << tmpv2 << " tmppush: " << tmppush; //temp.v1.push_back(tmppush); temp.set(tmppush, i); //v1.push_back(tmppush); //temp.PokazCalosc(); } return temp; } int main () { /* TabBit b; b.PokazCalosc(); cout << "Rozmiar: " << b.PobierzRozmiar() << endl; b.set(1,6); b.PokazCalosc(); b.set(1,19); b.PokazCalosc(); b.set(1,3); b.PokazCalosc(); b.set(0,19); b.PokazCalosc(); int nr_bitu = 6; cout << "\nBit numer " << nr_bitu << " ustawiony jest na " << b.get(nr_bitu); nr_bitu = 19; cout << "\nBit numer " << nr_bitu << " ustawiony jest na " << b.get(nr_bitu); nr_bitu = 3; cout << "\nBit numer " << nr_bitu << " ustawiony jest na " << b.get(nr_bitu); */ TabBit abc1; TabBit abc2; abc1.set(1,6); abc2.set(1,4); abc2.set(1,5); abc2.set(1,6); abc1.PokazCalosc(); abc2.PokazCalosc(); TabBit abc3 = abc1 & abc2; abc3.PokazCalosc(); while (!kbhit()); return 0; } ======================================================== #include <algorithm> #include <vector> #include <iostream.h> #include <string.h> #include <conio.h> typedef int element; class TabBit { public: TabBit(); void PokazCalosc(); int PobierzRozmiar(); bool set(bool zerojeden, int pozycja); bool get(int pozycja); vector<int> v1; private: int rozmiar; int maska; }; TabBit::TabBit() { this->rozmiar = 24; for(int i=0; i<=this->rozmiar; i++) { this->v1.push_back(0); } } bool TabBit::set(bool zerojeden, int pozycja) { // wstawienie pod dana pozycje jedynke lub zero //cout << endl << "Ustawienie bitu " << pozycja << " na " << zerojeden << endl; int nrbytu = pozycja / this->rozmiar; int nrbitu = pozycja % this->rozmiar; this->maska = 1<<nrbitu; int ktory = (nrbytu+nrbitu); //cout<<"ktory: " << ktory << " e: "<< this->v1[ktory]; if(zerojeden) this->v1[ktory] = this->v1[ktory] | this->maska; else this->v1[ktory] = this->v1[ktory] &~ this->maska; //cout<<" e2: "<< this->v1[ktory]; return true; } void TabBit::PokazCalosc() { cout << endl << "Zawartosc: "; for(int i=this->rozmiar; i>0; i--) { bool tmp = this->v1[i]; cout << tmp; if((i%8)==1) cout << " "; } cout << endl; } int TabBit::PobierzRozmiar() { return this->v1.size(); } bool TabBit::get(int pozycja) { return this->v1[pozycja]; } TabBit operator & (TabBit t1, TabBit t2) { TabBit temp; bool tmpv1, tmpv2; bool tmppush=0; for(int i=0; i<t1.PobierzRozmiar(); i++) { tmpv1 = t1.v1[i]; tmpv2 = t2.v1[i]; tmppush = tmpv1&tmpv2; temp.set(tmppush, i); } return temp; } TabBit operator | (TabBit t1, TabBit t2) { TabBit temp; bool tmpv1, tmpv2; bool tmppush=0; for(int i=0; i<t1.PobierzRozmiar(); i++) { tmpv1 = t1.v1[i]; tmpv2 = t2.v1[i]; tmppush = tmpv1|tmpv2; temp.set(tmppush, i); } return temp; } int main () { /* TabBit b; b.PokazCalosc(); cout << "Rozmiar: " << b.PobierzRozmiar() << endl; b.set(1,6); b.PokazCalosc(); b.set(1,19); b.PokazCalosc(); b.set(1,3); b.PokazCalosc(); b.set(0,19); b.PokazCalosc(); int nr_bitu = 6; cout << "\nBit numer " << nr_bitu << " ustawiony jest na " << b.get(nr_bitu); nr_bitu = 19; cout << "\nBit numer " << nr_bitu << " ustawiony jest na " << b.get(nr_bitu); nr_bitu = 3; cout << "\nBit numer " << nr_bitu << " ustawiony jest na " << b.get(nr_bitu); */ TabBit abc1; TabBit abc2; abc1.set(1,3); abc1.set(1,6); abc1.set(1,16); abc1.set(1,17); abc1.set(1,22); abc2.set(1,4); abc2.set(1,5); abc2.set(1,6); abc2.set(1,17); abc2.set(1,21); abc2.set(1,22); TabBit abc3 = abc1 & abc2; TabBit abc4 = abc1 | abc2; cout << "\npierwszy: "; abc1.PokazCalosc(); cout << "\ndrugi: "; abc2.PokazCalosc(); cout << "\npierwszy & drugi: "; abc3.PokazCalosc(); cout << "\npierwszy | drugi: "; abc4.PokazCalosc(); while (!kbhit()); return 0; }