Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

69 строки
1.3 KiB
C++

#include <stdio.h>
#include <stdint.h>
#include <locale.h>
#include <cstdlib>
uint16_t reverse(uint16_t x)
{
x = (x & 0xFF) << 8 | (x & 0xFF00) >> 8;
return x;
}
void print_in_hex(uint16_t num)
{
printf("%04X ", num);
}
void print_in_binary(uint16_t num)
{
for (int i = 15; i >= 0; i--)
{
printf("%d", (num >> i) & 1);
if (i % 8 == 0)
printf(" ");
}
}
int main()
{
uint16_t operand1, operand2;
char symbol;
printf("Enter the first operand, then the operator(&, | or ^), then the second operand.\n");
scanf("%hu %c %hu", &operand1, &symbol, &operand2);
operand1 = reverse(operand1);
operand2 = reverse(operand2);
uint16_t result;
switch (symbol)
{
case '&':
result = operand1 & operand2;
break;
case '|':
result = operand1 | operand2;
break;
case '^':
result = operand1 ^ operand2;
break;
default:
printf("Invalid operator\n");
return 1;
}
printf("\nResult: ");
print_in_hex(operand1);
printf("%c ", symbol);
print_in_hex(operand2);
printf("= ");
print_in_binary(operand1);
printf("%c ", symbol);
print_in_binary(operand2);
printf("= ");
print_in_binary(result);
printf("\n");
system("pause");
return 0;
}