Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. // //
  2. // // BitString.h
  3. // // LABA1
  4. // //
  5. // // Created by kosha on 28/09/2019.
  6. // // Copyright © 2019 kosha. All rights reserved.
  7. // //
  8. #include <string>
  9. #include <iostream>
  10.  
  11. class BitString128 {
  12. public:
  13. unsigned long high=0, low=0;
  14.  
  15. BitString128() = default;
  16.  
  17. BitString128(std::string num){
  18. int j =0;
  19. for (auto i = num.rbegin(); i!=num.rend(); ++i, ++j){
  20. j<64? low += (*i - '0')<<j : high += (*i - '0')<<(j-64);
  21. }
  22. }
  23.  
  24. const BitString128 operator~() const{
  25. BitString128 res = *this;
  26. res.high = ~res.high;
  27. res.low = ~res.low;
  28. return res;
  29. }
  30.  
  31. BitString128& operator^=(const BitString128& rhs){
  32. high ^= rhs.high;
  33. low ^= rhs.low;
  34. return *this;
  35. }
  36.  
  37. friend const BitString128 operator^(const BitString128& rhs, const BitString128& lhs){
  38. BitString128 res = lhs;
  39. return res^=rhs;
  40. }
  41.  
  42. BitString128& operator|=(const BitString128& rhs){
  43. high |= rhs.high;
  44. low |= rhs.low;
  45. return *this;
  46. }
  47.  
  48. friend const BitString128 operator|(const BitString128& rhs, const BitString128& lhs){
  49. BitString128 res = lhs;
  50. return res|=rhs;
  51. }
  52.  
  53. BitString128& operator&=(const BitString128& rhs){
  54. high &= rhs.high;
  55. low &= rhs.low;
  56. return *this;
  57. }
  58.  
  59. friend const BitString128 operator&(const BitString128& rhs, const BitString128& lhs){
  60. BitString128 res = lhs;
  61. return res &= rhs;;
  62. }
  63.  
  64. BitString128& operator>>=(int rhs){
  65. for (int i = 0; i< rhs; ++i, r_bit_shift());
  66. return *this;
  67. }
  68.  
  69. friend const BitString128 operator>>(const BitString128& lhs, const int& rhs){
  70. BitString128 res = lhs;
  71. return res>>=rhs;
  72. }
  73.  
  74. BitString128& operator<<=(int rhs){
  75. for (int i = 0; i< rhs; ++i, l_bit_shift());
  76. return *this;
  77. }
  78.  
  79. friend const BitString128 operator<<(const BitString128& lhs, const int& rhs){
  80. BitString128 res = lhs;
  81. return res<<=rhs;
  82. }
  83.  
  84. bool operator<(const BitString128& rhs)const{
  85. return (high < rhs.high or (high == rhs.high and low < rhs.low));
  86. }
  87.  
  88. bool operator>(const BitString128& rhs)const{
  89. return rhs < *this;
  90. }
  91.  
  92. bool operator>=(const BitString128& rhs)const{
  93. return !(*this < rhs);
  94. }
  95.  
  96. bool operator<=(const BitString128& rhs)const{
  97. return !(*this > rhs);
  98. }
  99.  
  100. bool operator==(const BitString128& rhs)const{
  101. return high == rhs.high and low == rhs.low;
  102. }
  103.  
  104. bool operator!=(const BitString128& rhs)const{
  105. return !(*this == rhs);
  106. }
  107.  
  108. int get_bits() const{
  109. int count = 0;
  110. for (int i = 63; i >= 0; --i) count += (high >>i) & 1;
  111. for (int i = 63; i >= 0; --i) count += (low >>i) & 1;
  112. return count;
  113. }
  114.  
  115. int compare_by_bits(const BitString128& rhs)const{
  116. return this->get_bits() - rhs.get_bits();
  117. }
  118.  
  119. private:
  120. void l_bit_shift(){
  121. high <<= 1;
  122. high |= (low >>63) & 1;
  123. low <<= 1;
  124. }
  125.  
  126. void r_bit_shift(){
  127. low >>= 1;
  128. low |= (high & 1)<<63;
  129. high >>= 1;
  130. }
  131. };
  132.  
  133. std::ostream& operator<<(std::ostream& os, const BitString128& obj){
  134. for (int i = 63; i >= 0; --i) os << ((obj.high >>i) & 1);
  135. for (int i = 63; i >= 0; --i) os << ((obj.low >>i) & 1);
  136. os << "\n";
  137. return os;
  138. }
  139.  
  140. std::istream& operator>>(std::istream& is, BitString128& obj){
  141. std::string input_number;
  142. is >> input_number;
  143. obj = BitString128(input_number);
  144. return is;
  145. }
  146.  
  147.  
  148. int main(){
  149. BitString128 num;
  150. std::cin >> num;
  151.  
  152. std::cout << (BitString128(std::string("110")) & std::string("111"))
  153. << (std::string("10") & BitString128(std::string("111")))
  154. << (std::string("111101010") ^ ~BitString128(std::string("101010000000010111111")));
  155.  
  156. std::cout<< num;
  157. std::cout<< (~num);
  158. std::cout<< (~num ^ num);
  159. std::cout<< (~num & num);
  160. std::cout<< (num >(num>>1))<<"\n";
  161. std::cout<< (num < (num<<2))<<"\n";
  162. std::cout<< num.get_bits()<<"\n";
  163. std::cout<< num.compare_by_bits(~num)<<"\n";
  164. return 0;
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement