Программа на языке Си для преобразования десятичной дроби в двоичную с помощью побитового оператора and, shift
this code is an example from a book that the problem require to change decimal to binary number using bitwise AND oeprator and shift operator. i cannot understand the code although had tried to understand this code using debug compiler. suppose for a and b, user input is 10 and 8
#include <stdio.h> #include <stdlib.h> int count_bits (unsigned x) { int bits=0; while(x){ if (x&1U)bits++; x>>=1; } return bits; } int int_bits(void) { return count_bits(~0U); } void print_bits(unsigned x){ int i; for(i=int_bits(x)-1;i>=0;i--) putchar(((x>>i)&1U)?'1':'0'); } int main(void) { unsigned a,b; /*suppose user input a=10 b=8*/ printf("enter two positive integer value=\n"); printf("a= "); scanf("%u",&a); printf("b: "); scanf("%u",&b); printf("\na ="); print_bits(a); printf("\na ="); print_bits(b); return 0; }
Что я уже пробовал:
in int_bits function what actually (~0U) does? <pre> what is the value of x in count_bits(unsigned x) function? and what is being compare in (x & 1U) and what is the relation of count_bits(~0U) and user input? in print_bits function in putchar (((x>>i)&1u)?'1';'0'); what is the value of x and i? i got i=32 as bits from count_bits
что на самом деле делает эта программа для получения двоичного числа?