Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <limits.h>
- #include <locale.h>
- short BinToDec(char bin[]);
- void DecToBin(short dec, char bin[CHAR_BIT + 1]);
- int main()
- {
- setlocale(LC_CTYPE, "Russian");
- printf("Введите две строки: ");
- char bin[2][CHAR_BIT + 1];
- char bin1[2][CHAR_BIT + 1];
- short dec[2];
- scanf("%s %s", bin[0], bin[1]);
- dec[0] = BinToDec(bin[0]);
- dec[1] = BinToDec(bin[1]);
- DecToBin(~dec[0], bin1[0]);
- DecToBin(~dec[1], bin1[1]);
- printf("~%s = %s\n", bin[0], bin1[0]);
- printf("~%s = %s\n\n", bin[1], bin1[1]);
- DecToBin(dec[0] & dec[1], bin1[0]);
- printf("%s & %s = %s\n", bin[0], bin[1], bin1[0]);
- DecToBin(dec[0] | dec[1], bin1[0]);
- printf("%s | %s = %s\n", bin[0], bin[1], bin1[0]);
- DecToBin(dec[0] ^ dec[1], bin1[0]);
- printf("%s ^ %s = %s\n", bin[0], bin[1], bin1[0]);
- return 0;
- }
- short BinToDec(char bin[])
- {
- int i = 0, l = strlen(bin);
- short result = 0;
- while (bin[i] != '\0')
- {
- if (bin[i] == '1') result += (short)(1 << (l - i - 1));
- i++;
- }
- return result;
- }
- void DecToBin(short dec, char bin[CHAR_BIT + 1])
- {
- int i = CHAR_BIT - 1, k = 0;
- int start_zero = 0;
- while (i >= 0)
- {
- if (((dec >> i) & 1) == 1) start_zero = 1;
- if (start_zero == 1)
- {
- bin[k] = '0' + ((dec >> i) & 1);
- k++;
- }
- i--;
- }
- bin[k] = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement