Given a floating point number, write a function to count set bits in its binary representation.
For example, floating point representation of 0.15625 has 6 set bits (See this). A typical C compiler uses single precision floating point format.
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.
#include <stdio.h> // A utility function to count set bits in a char. // Refer http://goo.gl/eHF6Y8 for details of this function. unsigned int countSetBitsChar(char n) { unsigned int count = 0; while (n) { n &= (n-1); count++; } return count; } // Returns set bits in binary representation of x unsigned int countSetBitsFloat(float x) { // Count number of chars (or bytes) in binary representation of float unsigned int n = sizeof(float)/sizeof(char); // typcast address of x to a char pointer char *ptr = (char *)&x; int count = 0; // To store the result for (int i = 0; i < n; i++) { count += countSetBitsChar(*ptr); ptr++; } return count; } // Driver program to test above function int main() { float x = 0.15625; printf ("Binary representation of %f has %u set bits ", x, countSetBitsFloat(x)); return 0; } |
Output:
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Convert a floating point number to string in C
- Rounding Floating Point Number To two Decimal Places in C and C++
- Write a one line C function to round floating point numbers
- Can we use % operator on floating point numbers?
- Floating Point Operations & Associativity in C, C++ and Java
- Precision of floating point numbers in C++ (floor(), ceil(), trunc(), round() and setprecision())
- C++ Floating Point Manipulation (fmod(), remainder(), remquo() ... in cmath)
- C Program to Multiply two Floating Point Numbers
- Problem in comparing Floating point numbers and how to compare them correctly?
- Rotation of a point about another point in C++
- sizeof() for Floating Constant in C
- 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
- Find the node whose sum with X has minimum set bits
- Maximize the number by rearranging bits
- Equation of straight line passing through a given point which bisects it into two equal line segments
- Count number of unique Triangles using STL | Set 1 (Using set)
- <bits/stdc++.h> in C++
- int (1 sign bit + 31 data bits) keyword in C
- Add two unsigned numbers using bits

