Click here to Skip to main content
15,887,444 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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;
}


What I have tried:

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

what actually this program do to produce the binary number?
Posted
Updated 13-May-17 7:16am
v4

C++
int int_bits(unsigned x)
{
    return count_bits(~0U);
}

That looks wrong, since it will return the same value for any value of x . I suggest changing it to:
C++
return count_bits(x);
 
Share this answer
 
Comments
Member 13188016 13-May-17 9:31am    
Ah sorry the correct one is int_bits (void) {
Return count_bits(~0U)
}
But still dont understand how it produce 1010 for 10
And what to compare to make it produce 1010
Richard MacCutchan 13-May-17 10:23am    
The decimal number 10 is equivalent to 1010 in binary. If you do not understand why then you need to read up on numbering systems.
Quote:
C program to convert decimal to binary using bitwise and, shift operator

The program do not convert, it just display the value in base 2.
Note that internally, integers are stored in binary in C programs.

To know what are & and ~, just read the documentation about 'bitwise operators'.
To know what is U, read documentation about 'integer constant'.
Quote:
i cannot understand the code although had tried to understand this code using debug compiler.

You will better understand by using the debugger at machine language level to see exactly how source code is translated.
 
Share this answer
 
Comments
Member 13188016 13-May-17 14:38pm    
Can i see return value in debugger?
How to debug at machine language level?
Because some debugger only allow to watch value of variable

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900