Program For Conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation
Program For Conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation
Related Articles
Write a program to find out the 32 Bits Single Precision IEEE 754 Floating-Point
Examples :
WeOutput:
use cookies0to ensure
| 10000011 | best
you have the 00001100000000000000000
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 1/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
Output: 16.75
Approach:
This implementation is based on Union Datatype in C and using the concept of Bit Fields.
Bit Fields are assigned when we don’t require the full memor y that is usually allocated to
some variables but we want to limit the amount of memor y taken up by those variables. In
C, members of a Union share the common memor y space and taken we can access the
C++
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 2/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
void printBinary(int n, int i)
{
// Prints the binary representation
// of a number n up to i-bits.
int k;
for (k = i - 1; k >= 0; k--) {
if ((n >> k) & 1)
printf("1");
else
printf("0");
}
}
typedef union {
float f;
struct
{
// Order is important.
// Here the members of the union data structure
// use the same memory (32 bits).
// The ordering is taken
// from the LSB to the MSB.
unsigned int mantissa : 23;
unsigned int exponent : 8;
unsigned int sign : 1;
} raw;
} myfloat;
// Function to convert real value
// to IEEE floating point representation
void printIEEE(myfloat var)
{
// Prints the IEEE 754 representation
// of a float value (32 bits)
printf("%d | ", var.raw.sign);
printBinary(var.raw.exponent, 8);
printf(" | ");
printBinary(var.raw.mantissa, 23);
printf("\n");
}
// Driver Code
We
intusemain()
cookies to ensure you have the best browsing experience on our website. By using our Got It !
site,
{ you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 4/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
// Instantiate the union
myfloat var;
// Get the real value
var.f = -2.25;
// Get the IEEE floating point representation
printf("IEEE 754 representation of %f is : \n",
var.f);
printIEEE(var);
return 0;
}
P ython3
# Function to get sign bit,
# exp bits and mantissa bits,
# from given real no.
def floatingPoint(real_no):
# Setting Sign bit
# default to zero.
sign_bit = 0
# Sign bit will set to
# 1 for negative no.
if(real_no < 0):
sign_bit = 1
# converting given no. to
# absolute value as we have
# already set the sign bit.
real_no = abs(real_no)
# Converting Integer Part
# of Real no to Binary
int_str = bin(int(real_no))[2 : ]
# Function call to convert
# Fraction part of real no
# to Binary.
fraction_str = binaryOfFraction(real_no - int(real_no))
# Getting the index where
# Bit was high for the first
# Time in binary repres
# of Integer part of real no.
ind = int_str.index('1')
# The Exponent is the no.
# By which we have right
# Shifted the decimal and
# it is given below.
# Also converting it to bias
# exp by adding 127.
exp_str = bin((len(int_str) - ind - 1) + 127)[2 : ]
# getting mantissa string
# By adding int_str and fraction_str.
# the zeroes in MSB of int_str
# have no significance so they
# are ignored by slicing.
mant_str = int_str[ind + 1 : ] + fraction_str
We use cookies
# Addingto ensure you have
Zeroes in the
LSBbestofbrowsing experience on our website. By using our Got It !
site,
# mantissa string so as and
you
acknowledge that you have read to understood
make our
Cookie Policy &
Privacy Policy
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 6/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
Output :
Ad
Keto Diet
We
use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 7/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
C++
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 8/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
// Convert the least significant
// mantissa part (23 bits)
// to corresponding decimal integer
unsigned int f = convertToInt(ieee, 9, 31);
// Assign integer representation of mantissa
var.raw.mantissa = f;
// Convert the exponent part (8 bits)
// to a corresponding decimal integer
f = convertToInt(ieee, 1, 8);
// Assign integer representation
// of the exponent
var.raw.exponent = f;
// Assign sign bit
var.raw.sign = ieee[0];
cout << "The float value of the given"
" IEEE-754 representation is : \n";
cout << fixed << setprecision(6) << var.f <<endl;
return 0;
}
// This code is contributed by ShubhamSingh10
// C program to convert
// IEEE 754 floating point representation
// into real value
#include <math.h>
#include <stdio.h>
typedef union {
float f;
struct
{
// Order is important.
// Here the members of the union data structure
// use the same memory (32 bits).
// The ordering is taken
// from the LSB to the MSB.
We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
unsigned int mantissa : 23;
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 9/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 10/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
P ython3
Output :
-2.250000
Attention reader! Don’t stop learning now. Get hold of all the impor tant CS Theor y
concepts for SDE inter views with the CS Theor y Course at a student-friendly price and
We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 12/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
Like 0
Previous Next
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 13/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
Ar ticle Contributed By :
Current difficulty :
Medium
Load Comments
We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 14/15
9/5/21, 1:00 AM Program for conversion of 32 Bits Single Precision IEEE 754 Floating Point Representation - GeeksforGeeks
Company Learn
About Us Algorithms
Careers Data Structures
Privacy Policy Languages
Contact Us CS Subjects
Copyright Policy Video Tutorials
@geeksforgeeks
, Some rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our Got It !
site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy
https://www.geeksforgeeks.org/program-for-conversion-of-32-bits-single-precision-ieee-754-floating-point-representation/ 15/15