How to count set bits in a floating point number in C? (original) (raw)
Last Updated : 21 Jun, 2022
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.
C `
#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);
// typecast 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
Time Complexity: O(nlogn)
Auxiliary Space: O(1)
Similar Reads
- Convert a floating point number to string in C Write a C function ftoa() that converts a given floating-point number or a double to a string. Use of standard library functions for direct conversion is not allowed. The following is prototype of ftoa(). The article provides insight of conversion of C double to string. ftoa(n, res, afterpoint) n -- 3 min read
- C/C++ Program to Count set bits in an integer Write an efficient program to count number of 1s in binary representation of an integer. Examples :Input : n = 6Output : 2Binary representation of 6 is 110 and has 2 set bitsInput : n = 13Output : 3Binary representation of 11 is 1101 and has 3 set bits1. Simple Method Loop through all bits in an int 2 min read
- Write a one line C function to round floating point numbers Algorithm: roundNo(num) 1. If num is positive then add 0.5. 2. Else subtract 0.5. 3. Type cast the result to int and return. Example: num = 1.67, (int) num + 0.5 = (int)2.17 = 2 num = -1.67, (int) num - 0.5 = -(int)2.17 = -2 Implementation: c /* Program for rounding floating point numbers */ # inclu 1 min read
- Count number of bits to be flipped to convert A to B | Set-2 Given two integers A and B, the task is to count the number of bits needed to be flipped to convert A to B.Examples: Input: A = 10, B = 7 Output: 3 binary(10) = 1010 binary(7) = 0111 1010 0111 3 bits need to be flipped.Input: A = 8, B = 7 Output: 4 Approach: An approach to solve this problem has alr 5 min read
- Sign Extend a Nine-Bit Number in C Prerequisite: Bitwise operators in CIn this article, we will see what is Sign extension and how to significantly extend a nine-bit number with a C program. This method can be used for signing to extend a nine-bit number to 32-bit, extend nine-bit to 64-bit, extend 16-bit to 32-bit, etc. It follows t 3 min read
- Counting Set Bits in C++ Counting set bits means determining how many bits in a binary representation of a number are set to 1.ExampleInput: n = 103 (01100111)Output: 5Explanation: The binary representation of 103 is 01100111. Hence, the number of set bits is 5.Input: n = 15 (1111)Output: 4Explanation: The binary representa 4 min read
- sizeof() for Floating Constant in C In C language, we have three floating data types i.e. float, double and long double. And the exact size of each of these 3 types depends on the C compiler implementation/platform. The following program can be used to find out the size of each floating data type on your machine. C #include " 2 min read
- Counting Set bit in C In C programming, counting set bits is the process of determining the number of bits that are set to 1 in a binary number. This operation is useful in various applications including network programming, error detection, and cryptography.In this article, we will learn how to count the number of set b 4 min read
- How to print range of basic data types without any library function and constant in C? How to write C code to print range of basic data types like int, char, short int, unsigned int, unsigned char etc? It is assumed that signed numbers are stored in 2's complement form. We strongly recommend to minimize the browser and try this yourself first. Following are the steps to be followed fo 4 min read
- bitset set() function in C++ STL bitset::set() is a built-in STL in C++ which sets the bit to a given value at a particular index. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that particular index to 1. Syntax: set(int index, bool val) Parameter: The function accepts tw 2 min read