-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path31_IEEE_printbits.c
99 lines (89 loc) · 2.34 KB
/
31_IEEE_printbits.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/***************************************************************************************
NAME : Karthikeyan.V
DATE : 8.08.2021
DESCRIPTION : Print bits of float & double. Check IEEE std.
OUTPUT :
./a.out
ENTER THE CHOICE :
1.FLOAT
2.DOUBLE
CHOICE: 1
ENTER THE FLOAT VALUE: 2.3
SIGN EXPONENT MANTISSA
-----------------------------------------------
0 10000000 00100110011001100110011
DO YOU WANT TO CONTINUE(y/Y): y
ENTER THE CHOICE :
1.FLOAT
2.DOUBLE
CHOICE: 2
ENTER THE DOUBLE VALUE: -2.3
SIGN EXPONENT MANTISSA
-------------------------------------------------------------------------------
1 10000000000 0010011001100110011001100110011001100110011001100110
DO YOU WANT TO CONTINUE(y/Y): n
***************************************************************************************/
#include<stdio.h>
#include<stdio_ext.h>
void print_float(void *);
void print_double(void *);
int main()
{
char choice;
int operation;
float f;
double d;
do
{
printf("ENTER THE CHOICE :\n1.FLOAT\n2.DOUBLE\nCHOICE: ");
scanf(" %d",&operation);
switch(operation)
{
case 1:
printf("\nENTER THE FLOAT VALUE: ");
scanf("%f",&f);
printf("SIGN\tEXPONENT\tMANTISSA\n");
printf("-----------------------------------------------\n");
print_float(&f);
break;
case 2:
printf("\nENTER THE DOUBLE VALUE: ");
scanf("%lf",&d);
printf("SIGN\tEXPONENT\tMANTISSA\n");
printf("-------------------------------------------------------------------------------\n");
print_double(&d);
break;
default:
printf("\nERROR : ENTER A VALID OPERATION\n");
break;
}
printf("DO YOU WANT TO CONTINUE(y/Y): ");
__fpurge(stdin);
scanf(" %c",&choice);
} while (choice == 'y' || choice == 'Y');
return 0;
}
/*printing IEEE format of float*/
void print_float(void *ptr)
{
printf("%d\t",(*(int *)ptr) >> 31 & 1);
for(int i = 30; i >= 23; i--)
printf("%d",(*(int *)ptr) >> i & 1);
printf("\t");
for(int i = 22; i >= 0; i--)
printf("%d",(*(int *)ptr) >> i & 1);
printf("\n\n");
}
/*printing IEEE format of double*/
void print_double(void *ptr)
{
printf("%d\t",*(((int *)ptr)+1) >> 31 & 1);
for(int i = 30; i >= 20; i--)
printf("%d",*(((int *)ptr)+1) >> i & 1);
printf("\t");
for(int i = 19; i >= 0; i--)
printf("%d",*(((int *)ptr)+1) >> i & 1);
for(int i= 31; i >= 0; i--)
printf("%d",(*(int *)ptr) >> i & 1);
printf("\n\n");
}