Converting an integer to binary in C

后端 未结 12 1328
别跟我提以往
别跟我提以往 2021-02-02 04:30

I\'m trying to convert an integer 10 into the binary number 1010.

This code attempts it, but I get a segfault on the strcat():

int int_to_bin(int k)
{
          


        
12条回答
  •  天命终不由人
    2021-02-02 04:36

    The working solution for Integer number to binary conversion is below.

    int main()
    {
        int num=241; //Assuming 16 bit integer
        for(int i=15; i>=0; i--) cout<<((num >> i) & 1);
        cout<> i) & 1);
        cout<

    You can capture the cout<< part based on your own requirement.

提交回复
热议问题