Docsity

computer organisation and architecture experiment 1, Assignments of Computer Architecture and Organization

Partial preview of the text

Download computer organisation and architecture experiment 1 and more Assignments Computer Architecture and Organization in PDF only on Docsity!

Experiment No.

Implement a program to convert hexadecimal, decimal

number to binary number

Date of Performance:

Date of Submission:

Aim: To Implement a program to convert hexadecimal, decimal number to binary number. Objective: To write programs in c to implement and understand the conversion of hexadecimal, decimal numbers to binary numbers Theory: Number Systems Overview: โ— Decimal (Base-10): The standard number system used in everyday life, using digits 0 โ€“ 9. โ— Hexadecimal (Base-16): Used commonly in computing, especially in memory addresses. Uses digits 0 โ€“ 9 and letters A โ€“ F. โ— Binary (Base-2): The machine language of computers, using only digits 0 and 1. Conversion Logic:

  1. Decimal to Binary Conversion: โ— Divide the decimal number by 2 repeatedly. โ— Record the remainders. โ— Reverse the remainders to get the binary equivalent. Example: Decimal 13 โ†’ 13 รท 2 = 6 remainder 1 โ†’ 6 รท 2 = 3 remainder 0 โ†’ 3 รท 2 = 1 remainder 1 โ†’ 1 รท 2 = 0 remainder 1 โ†’ Binary = 1101
  2. Hexadecimal to Binary Conversion: โ— Convert each hexadecimal digit to its 4-bit binary equivalent. Mapping Table:

#include <stdio.h> #include <string.h> #include <math.h> // Function to convert decimal to binary void decimalToBinary(int n) { int binary[32]; int i = 0; if (n == 0) { printf("Binary: 0\n"); return; } while (n > 0) { binary[i] = n % 2; n = n / 2; i++; } printf("Binary: "); for (int j = i - 1; j >= 0; j--) printf("%d", binary[j]); printf("\n"); } // Function to convert hexadecimal to binary void hexToBinary(char hex[]) { int i = 0;

printf("Binary: "); while (hex[i]) { switch (hex[i]) { case '0': printf("0000"); break; case '1': printf("0001"); break; case '2': printf("0010"); break; case '3': printf("0011"); break; case '4': printf("0100"); break; case '5': printf("0101"); break; case '6': printf("0110"); break; case '7': printf("0111"); break; case '8': printf("1000"); break; case '9': printf("1001"); break; case 'A': case 'a': printf("1010"); break; case 'B': case 'b': printf("1011"); break; case 'C': case 'c': printf("1100"); break; case 'D': case 'd': printf("1101"); break; case 'E': case 'e': printf("1110"); break; case 'F': case 'f': printf("1111"); break; default: printf("\nInvalid hexadecimal digit %c", hex[i]); return; }

Output: Conclusion: We have successfully implemented the conversion of hexadecimal numbers and decimal numbers to binary numbers using C programming.