Advertisement
Korotkodul

bitwise calcuoator

Sep 19th, 2024
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.32 KB | None | 0 0
  1. //
  2. // A bitwise logical expression is much like an arithmetic expression except
  3. // that the operators are ! (not), ~ (complement), & (and), | (or), and ^
  4. // (exclusive or). Each operator does its operation to each bit of its
  5. // integer operands (see §25.5). ! and ~ are prefix unary operators. A ^
  6. // binds tighter than a | (just as * binds tighter than +) so that x|y^z
  7. // means x|(y^z) rather than (x|y)^z. The & operator binds tighter than ^ so
  8. // that x^y&z means x^(y&z).
  9. //
  10.  
  11. /*
  12.   Grammar of a bitwise logical expression.
  13.  
  14.   Expression:
  15.     Term
  16.     Expression "|" Term
  17.   Term:
  18.     Subterm
  19.     Term "^" Subterm
  20.   Subterm:
  21.     Primary
  22.     Subterm "&" Primary
  23.   Primary:
  24.     Number
  25.     "(" Expression ")"
  26.     "!" Primary
  27.     "~" Primary
  28.   Number:
  29.     integer_literal
  30.  
  31. */
  32.  
  33. #include <std_lib_facilities.h>
  34.  
  35. int expression ()
  36. {
  37.   // to be implemented
  38. }
  39.  
  40. int main ()
  41. try
  42. {
  43.   int val{};
  44.  
  45.   while (cin)
  46.   {
  47.     Token t = ts.get();
  48.  
  49.     if (t.kind == 'q')
  50.       break;            // 'q' for quit
  51.     if (t.kind == ';')  // ';' for "print now"
  52.       cout << "=" << val << '\n';
  53.     else
  54.       ts.putback(t);
  55.  
  56.     val = expression();
  57.   }
  58. }
  59. catch (exception& e)
  60. {
  61.   cerr << "error: " << e.what() << '\n';
  62.   return 1;
  63. }
  64. catch (...)
  65. {
  66.   cerr << "Oops: unknown exception!\n";
  67.   return 2;
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement