Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
CO_assign_q1
#include <stdio.h> #include <string.h> #include <math.h> #define MAX 100 int arr[MAX]; int dec[MAX]; char num[MAX]; int store = 0; void Bin_Bits(unsigned n,int k,int ch) { //k refers to the max number of bits to be shown unsigned i; int count = 1; for(i = 1<<k-1; i > 0; i = i/2) { if(n & i) printf("1"); else printf("0"); if(ch == 1) { if(count % 8 == 0) //This is for printing in intervals of 8 bits at a time if the choice is 1 printf(" "); count++; } } printf(" "); } void BinaryCalculate(int n) { int i = 0,j; if(n == 0) { arr[i] = 0; num[store++] = '0'; num[store++] = '.'; return; }; while(n > 0) { arr[i++] = n % 2; n = n/2; } int length = i; for(j = length-1; j >= 0; j--) { if(arr[j] > 0) num[store++] ='1'; else num[store++]='0'; } num[store++] = '.'; } void Trunc_Decimal(float n) { int i = 0,count = 0,k; float temp = n,t; if(n == 0.0) { dec[i++] = 0; num[store++] = '0'; return; } while(count != 23) { dec[i++] = (int)(temp * 2); if((int)(temp * 2) == temp * 2) break; t = temp * 2; temp = t - (int)(t); count++; } for(k = 0; k < i; k++) { //printf("%d",dec[k]); if(dec[k]>0) num[store++] ='1'; else num[store++]='0'; } } int Shift(char s[]) { int i=0,j,count = 0; char t; int sign = s[0] - '0'; if(sign == 1) { for(i = 0; s[i] != '\0'; i++) { if(s[i] == '.') { j = i; while(j != 1) { t = s[j]; s[j] = s[j-1]; s[j-1] = t; count++; j--; } } } } else if(sign == 0) { i = 1; while(s[i-1] != '1') { t = s[i]; s[i] = s[i+1]; s[i+1] = t; i++; count--; } //printf("\n%d\n",count); } return count; } void Check_Mantissa(char s[],int ch) { int n,i,j,k; if(ch == 3) k = 23; else k = 52; if(s[0] == '0') { for(i = 0; s[i] != '\0'; i++) { if(s[i] == '.') { n = strlen(s) - i; for(j = i+1; s[j] != '\0'; j++) printf("%c",s[j]); } } } else { n = strlen(s) - 2; for(j = 2; s[j] != '\0'; j++) printf("%c",s[j]); } if(n < k) { for(i = 0; i < k - n; i++) printf("0"); } } int main() { int i,true_exp,biased_exp; float n,temp; int choice = 0; int n1; char ch; while(choice != 5) { printf("\nSelect an option\n"); printf("1. Integer representation\n"); printf("2. Character representation\n"); printf("3. Floating point representation\n"); printf("4. Double precision representation\n"); printf("5. Exit\n\n"); printf("Enter your choice\n"); scanf("%d,",&choice); if(choice == 1) { printf("Enter any integer\n"); scanf("%d",&n1); //if(n1 == 0) Bin_Bits(n1,32,choice); } else if(choice == 2) { printf("Enter any character\n"); scanf(" %c",&ch); Bin_Bits((int)ch,8,choice); } else if(choice == 3) { int flag1 = 0; store = 0; printf("Enter any number\n"); scanf("%f",&n); if(n > 0) BinaryCalculate(n); else if(n < 0) { BinaryCalculate(-n); flag1 = 1; } else { int s; printf("0 "); for(s = 0; s < 31; s++) { printf("0"); if(s == 7) printf(" "); } continue; } if(flag1 == 1) temp = (-n) - (int)(-n); else temp = n - (int)(n); Trunc_Decimal(temp); //printf("%s\n",num); true_exp = Shift(num);//to normalize the number //printf("%s\n",num);//to print final normalized number if(flag1 == 1) printf("\n1 "); else printf("\n0 "); biased_exp = true_exp + 127; Bin_Bits(biased_exp,8,choice);//to print the biased exponent Check_Mantissa(num,choice);//to make 23 bits for the mantissa printf("\n"); } else if(choice == 4) { int flag2 = 0; store = 0; printf("Enter any number\n"); scanf("%f",&n); if(n > 0) BinaryCalculate(n); else if (n < 0) { BinaryCalculate(-n); flag2 = 1; } else { int s; printf("0 "); for(s = 0; s < 72; s++) { printf("0"); if(s == 10) printf(" "); } continue; } if(flag2 == 1) temp = (-n) - (int)(-n); else temp = n - (int)(n); Trunc_Decimal(temp); //printf("%s\n",num); true_exp = Shift(num);//to normalize the number //printf("%s\n",num); if(flag2 == 1) printf("\n1 "); else printf("\n0 "); biased_exp = true_exp + 1023; Bin_Bits(biased_exp,11,choice);//to print the biased exponent Check_Mantissa(num,choice);//to make 23 bits for the mantissa printf("\n"); } } return 0; }
run
|
edit
|
history
|
help
0
pseudo hw
14th Dec Project1 v0.2
Assignment 5 Cos(x)
Singly LinkedList.c
summing long int
stringify
A_141107_Potencia
A141212_OrdenarArrayPorFunciones
date diff sample
swap two numbers without arithematic operators