Given a floating point number, write a function to count set bits in its binary representation.
We can use the idea discussed here. The idea is to take address of the given floating point number in a pointer variable, typecast the pointer to char * type and process individual bytes one by one. We can easily count set bits in a char using the techniques discussed here.
Following is C implementation of the above idea.
Binary representation of 0.156250 has 6 set bits
This article is contrbuted by Vineet Gupta. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Convert a floating point number to string in C
- Rounding Floating Point Number To two Decimal Places in C and C++
- Can we use % operator on floating point numbers?
- C Program to Multiply two Floating Point Numbers
- Floating Point Operations & Associativity in C, C++ and Java
- Write a one line C function to round floating point numbers
- C++ Floating Point Manipulation (fmod(), remainder(), remquo() ... in cmath)
- Problem in comparing Floating point numbers and how to compare them correctly?
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- Maximize the number by rearranging bits
- Find a number containing N - 1 set bits at even positions from the right
- Program to find the Nth natural number with exactly two bits set
- sizeof() for Floating Constant in C
- Rotation of a point about another point in C++
- Count number of unique Triangles using STL | Set 1 (Using set)
- Count the number of 1's and 0's in a binary array using STL in C++ ?
- C Program to count number of lines in a file
- C Program to count the Number of Characters in a File
- Lex program to count the number of lines, spaces and tabs
- Count the number of objects using Static member function