Good day. There is a task for bitwise operations. Conditions of the problem:

  1. Calculate R = A & B and check the transfer to decimal midrange.
  2. Calculate R = A | B and check the transfer to decimal midrange.
  3. Calculate R = A ^ B, Q = R ^ B and check by transferring to decimal midrange.
  4. Calculate R = ~ A, Q = ~ R.
  5. Calculate A >> c, B >> d and check by division.
  6. Calculate A << d, B << c and check by multiplication.

I, with my knowledge, could only make the first condition (Calculate R = A & B and check by transferring to decimal midrange), and that is not completely. Below is my code. How can it immediately implement all the conditions of the problem with the translation into the decimal MF? Thank you in advance.

#include "stdafx.h" #include <locale.h> #include <conio.h> int main() { setlocale(0,""); int A, B; printf("\nEnter A: A = "); scanf_s("%d",&A); printf("\nEnter B: B = "); scanf_s("%d",&B); printf("\nA & B = %d",A&B); getch(); return 0; } 
  • one
    The rest is completely similar, it is not clear what your problems are. True, I do not understand how the transfer to the decimal number system will help in checking ... - Vladimir Martyanov
  • one
    What is the problem with the rest of the operations? They, like A&B only need to write A|B , A^B , ~A , A>>c , A<<d . You can convert to the decimal system, but you already did printf along with %d ( %d means decimal , that is, the decimal number system). - Arty OneSoul

1 answer 1

 #include <bitset> #include <iostream> using namespace std; int main(int argc, const char * argv[]) { cout << "Π‘ΠΈΡ‚ΠΎΠ²Ρ‹Π΅ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ Π½Π° Π‘++" << endl; cout << "-----------------------" << endl << endl; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число А:" << endl; // Π’Π²ΠΎΠ΄ числа А int a; cin >> a; cout << endl; // Π’Π²ΠΎΠ΄ числа B int b; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число B:" << endl; cin >> b; cout << endl; // ВычислСниС R ΠΏΡƒΠ½ΠΊΡ‚ 1 int r = a & b; cout << "R = A & B = " << r << endl; cout << "A = " << bitset<16>(a) << endl; cout << "B = " << bitset<16>(b) << endl; cout << "R = " << bitset<16>(r) << endl; cout << endl; // ВычислСниС R ΠΏΡƒΠ½ΠΊΡ‚ 2 r = a | b; cout << "R = A | B = " << r << endl; cout << "A = " << bitset<16>(a) << endl; cout << "B = " << bitset<16>(b) << endl; cout << "R = " << bitset<16>(r) << endl; cout << endl; // ВычислСниС R ΠΈ Q ΠΏΡƒΠ½ΠΊΡ‚ 3 r = a ^ b; cout << "R = A ^ B = " << r << endl; cout << "A = " << bitset<16>(a) << endl; cout << "B = " << bitset<16>(b) << endl; cout << "R = " << bitset<16>(r) << endl; cout << endl; int q = r ^ b; cout << "Q = R ^ B = " << q << endl; cout << "R = " << bitset<16>(r) << endl; cout << "B = " << bitset<16>(b) << endl; cout << "Q = " << bitset<16>(q) << endl; cout << endl; // 4. Π’Ρ‹Ρ‡ΠΈΡΠ»ΠΈΡ‚ΡŒ R = ~ A, Q = ~ R. r = ~a; cout << "R = ~A = " << r << endl; cout << "A = " << bitset<16>(a) << endl; cout << "R = " << bitset<16>(r) << endl; cout << endl; q = ~r; cout << "Q = ~R = " << q << endl; cout << "R = " << bitset<16>(r) << endl; cout << "Q = " << bitset<16>(q) << endl; cout << endl; //5. Π’Ρ‹Ρ‡ΠΈΡΠ»ΠΈΡ‚ΡŒ A >> c, B >> d ΠΈ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ Π΄Π΅Π»Π΅Π½ΠΈΠ΅ΠΌ. // Π’Π²ΠΎΠ΄ числа c int c; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число c:" << endl; do { cin >> c; } while (c < 0); cout << endl; // Π’Π²ΠΎΠ΄ числа d int d; cout << "Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число d:" << endl; do { cin >> d; } while (d < 0); cout << endl; //6 .Π’Ρ‹Ρ‡ΠΈΡΠ»ΠΈΡ‚ΡŒ A << d, B << c ΠΈ ΠΏΡ€ΠΎΠ²Π΅Ρ€ΠΈΡ‚ΡŒ ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ΠΌ. int e1 = a << d; cout << "e = A << d = " << e1 << endl; cout << "A = " << bitset<16>(a) << endl; cout << "d = " << d << endl; cout << "e = " << bitset<16>(e1) << endl; cout << endl; int e2 = b << c; cout << "e = B << c = " << e2 << endl; cout << "B = " << bitset<16>(b) << endl; cout << "c = " << c << endl; cout << "e = " << bitset<16>(e2) << endl; cout << endl; cout << "ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ΠΌ:" << endl; int f = a; for (int i = 0; i<d; i++) { f *= 2; } int g = b; for (int i = 0; i<c; i++) { g *= 2; } cout << "e1 сдвиг = " << bitset<16>(e1) << endl; cout << "e1 ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ = " << bitset<16>(f) << endl; cout << "e2 сдвиг = " << bitset<16>(e2) << endl; cout << "e2 ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ = " << bitset<16>(g) << endl; return 0; } // OUTPUT: Π‘ΠΈΡ‚ΠΎΠ²Ρ‹Π΅ ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΈ Π½Π° Π‘++ ----------------------- Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число А: 3 Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число B: 4 R = A & B = 0 A = 0000000000000011 B = 0000000000000100 R = 0000000000000000 R = A | B = 7 A = 0000000000000011 B = 0000000000000100 R = 0000000000000111 R = A ^ B = 7 A = 0000000000000011 B = 0000000000000100 R = 0000000000000111 Q = R ^ B = 3 R = 0000000000000111 B = 0000000000000100 Q = 0000000000000011 R = ~A = -4 A = 0000000000000011 R = 1111111111111100 Q = ~R = 3 R = 1111111111111100 Q = 0000000000000011 Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число c: 2 Π’Π²Π΅Π΄ΠΈΡ‚Π΅ число d: 3 e = A << d = 24 A = 0000000000000011 d = 3 e = 0000000000011000 e = B << c = 16 B = 0000000000000100 c = 2 e = 0000000000010000 ΠŸΡ€ΠΎΠ²Π΅Ρ€ΠΊΠ° ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ΠΌ: e1 сдвиг = 0000000000011000 e1 ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ = 0000000000011000 e2 сдвиг = 0000000000010000 e2 ΡƒΠΌΠ½ΠΎΠΆΠ΅Π½ΠΈΠ΅ = 0000000000010000