ในปัญหานี้ มีค่าทศนิยมหนึ่งค่า เราต้องหาจำนวนเซ็ตบิตในการแทนค่าไบนารี่ของมัน
ตัวอย่างเช่น หากจำนวนทศนิยมคือ 0.15625 จึงมีชุดบิตหกชุด คอมไพเลอร์ C ทั่วไปใช้การแสดงจุดลอยตัวที่มีความแม่นยำเพียงจุดเดียว ก็จะได้หน้าตาประมาณนี้

ในการแปลงเป็นค่าบิต เราต้องนำตัวเลขไปเป็นตัวแปรตัวชี้หนึ่งตัว จากนั้นพิมพ์ตัวชี้ไปที่ข้อมูลประเภท char* จากนั้นประมวลผลทีละไบต์ จากนั้นเราสามารถนับเซตบิตของแต่ละตัวอักษรได้
ตัวอย่าง
#include <stdio.h>
int char_set_bit_count(char number) {
unsigned int count = 0;
while (number != 0) {
number &= (number-1);
count++;
}
return count;
}
int count_float_set_bit(float x) {
unsigned int n = sizeof(float)/sizeof(char); //count number of characters in the binary equivalent
int i;
char *ptr = (char *)&x; //cast the address of variable into char
int count = 0; // To store the result
for (i = 0; i < n; i++) {
count += char_set_bit_count(*ptr); //count bits for each bytes ptr++;
}
return count;
}
main() {
float x = 0.15625;
printf ("Binary representation of %f has %u set bits ", x, count_float_set_bit(x));
} ผลลัพธ์
Binary representation of 0.156250 has 6 set bits