Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // //
- // // BitString.h
- // // LABA1
- // //
- // // Created by kosha on 28/09/2019.
- // // Copyright © 2019 kosha. All rights reserved.
- // //
- #include <string>
- #include <iostream>
- class BitString128 {
- public:
- unsigned long high=0, low=0;
- BitString128() = default;
- BitString128(std::string num){
- int j =0;
- for (auto i = num.rbegin(); i!=num.rend(); ++i, ++j){
- j<64? low += (*i - '0')<<j : high += (*i - '0')<<(j-64);
- }
- }
- const BitString128 operator~() const{
- BitString128 res = *this;
- res.high = ~res.high;
- res.low = ~res.low;
- return res;
- }
- BitString128& operator^=(const BitString128& rhs){
- high ^= rhs.high;
- low ^= rhs.low;
- return *this;
- }
- friend const BitString128 operator^(const BitString128& rhs, const BitString128& lhs){
- BitString128 res = lhs;
- return res^=rhs;
- }
- BitString128& operator|=(const BitString128& rhs){
- high |= rhs.high;
- low |= rhs.low;
- return *this;
- }
- friend const BitString128 operator|(const BitString128& rhs, const BitString128& lhs){
- BitString128 res = lhs;
- return res|=rhs;
- }
- BitString128& operator&=(const BitString128& rhs){
- high &= rhs.high;
- low &= rhs.low;
- return *this;
- }
- friend const BitString128 operator&(const BitString128& rhs, const BitString128& lhs){
- BitString128 res = lhs;
- return res &= rhs;;
- }
- BitString128& operator>>=(int rhs){
- for (int i = 0; i< rhs; ++i, r_bit_shift());
- return *this;
- }
- friend const BitString128 operator>>(const BitString128& lhs, const int& rhs){
- BitString128 res = lhs;
- return res>>=rhs;
- }
- BitString128& operator<<=(int rhs){
- for (int i = 0; i< rhs; ++i, l_bit_shift());
- return *this;
- }
- friend const BitString128 operator<<(const BitString128& lhs, const int& rhs){
- BitString128 res = lhs;
- return res<<=rhs;
- }
- bool operator<(const BitString128& rhs)const{
- return (high < rhs.high or (high == rhs.high and low < rhs.low));
- }
- bool operator>(const BitString128& rhs)const{
- return rhs < *this;
- }
- bool operator>=(const BitString128& rhs)const{
- return !(*this < rhs);
- }
- bool operator<=(const BitString128& rhs)const{
- return !(*this > rhs);
- }
- bool operator==(const BitString128& rhs)const{
- return high == rhs.high and low == rhs.low;
- }
- bool operator!=(const BitString128& rhs)const{
- return !(*this == rhs);
- }
- int get_bits() const{
- int count = 0;
- for (int i = 63; i >= 0; --i) count += (high >>i) & 1;
- for (int i = 63; i >= 0; --i) count += (low >>i) & 1;
- return count;
- }
- int compare_by_bits(const BitString128& rhs)const{
- return this->get_bits() - rhs.get_bits();
- }
- private:
- void l_bit_shift(){
- high <<= 1;
- high |= (low >>63) & 1;
- low <<= 1;
- }
- void r_bit_shift(){
- low >>= 1;
- low |= (high & 1)<<63;
- high >>= 1;
- }
- };
- std::ostream& operator<<(std::ostream& os, const BitString128& obj){
- for (int i = 63; i >= 0; --i) os << ((obj.high >>i) & 1);
- for (int i = 63; i >= 0; --i) os << ((obj.low >>i) & 1);
- os << "\n";
- return os;
- }
- std::istream& operator>>(std::istream& is, BitString128& obj){
- std::string input_number;
- is >> input_number;
- obj = BitString128(input_number);
- return is;
- }
- int main(){
- BitString128 num;
- std::cin >> num;
- std::cout << (BitString128(std::string("110")) & std::string("111"))
- << (std::string("10") & BitString128(std::string("111")))
- << (std::string("111101010") ^ ~BitString128(std::string("101010000000010111111")));
- std::cout<< num;
- std::cout<< (~num);
- std::cout<< (~num ^ num);
- std::cout<< (~num & num);
- std::cout<< (num >(num>>1))<<"\n";
- std::cout<< (num < (num<<2))<<"\n";
- std::cout<< num.get_bits()<<"\n";
- std::cout<< num.compare_by_bits(~num)<<"\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement